The random function ranges from 1> 10, generating the number required [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a random function between 1 and 100
I want it to random out 2 < x <10, x is the number I need, if x does not randomize the number in that range then continue random until satisfactory, how to write
Thank.
$x = rand(1,100);
if (2 < $x < 100){echo 'ok'; }
$x = rand(1,100);
if (2 < $x < 100){echo 'ok'; }

Just use a simple while loop:
$x = rand(1,100);
while (2 > $x || $x > 10) {
$x = rand(1,100);
}
echo $x;
The above loop will only stop if your number is between your specified range (not sure if I understood your needs correctly).
Another approach could be to simply use a different interval for the rand() function.
EDIT: Please check the comments of your question, there is a solution.

<?php
for (
$i = 0, $limit = 10;
$i < $limit && $x = rand(1,100);
$i++
) {
echo $x, ' ', (2 < $x && $x < 10) ? 'HIT' : 'MISS', "\n";
}
Example output:
76 MISS
83 MISS
59 MISS
13 MISS
7 HIT
18 MISS
9 HIT
39 MISS
79 MISS
91 MISS

Related

How to program loop with two different variables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Imagine you want to add the sum of 2 dice so the output looks like this in 6 lines:
2 3 4 5 6 7 3 4 5 6 7 8 9 4 5 6 7 8 9 10 11 5 6 ... 7 8... 8 9...
By only programming a single loop ?
I tried to do it with nested loops but could figure out the logic for it.
You can do with one while loop and two variables for two dices.
<?php
$x = 1;
$y = 1;
while ( $x <= 6 ) {
echo $x + $y;
$y++;
if ( $y > 6 ) {
$y = 1;
$x++;
}
}
It's essentially a mechanical counter. When the first wheel completes a rotation, it resets, and advances the second wheel. Except in this case they are dice. When $y is greater than six, it resets and advances $x.
You would use similar code when dealing with dates, advancing months when the days tick over etc.
The code would be simpler using two loops.
for ( $x = 1; $x <= 6; $x++ ) {
for ( $y = 1; $y <= 6; $y++ ) {
echo $x + $y;
}
}
As this was not allowed, the while loop is the 'first wheel' and the if statement determines when to reset and advance the second.
https://www.hackerrank.com/ is good if you want to practice solving coding problems

How To Solve This PHP Algorithm? [duplicate]

This question already has answers here:
Test if number is odd or even
(20 answers)
Closed 4 years ago.
I am trying to create this algorithm with PHP. I want to be able to echo out the result of this operation:
1 - 2 + 3 - 4 + 5 - 6 +...100
I want to get the result of this till I get to 100.
This how I have already started the code, however I am stuck and don't know how to proceed:
<?php
$somme = 0;
$I = 1;
while($I <= 100){
}
?>
How do I go on from this?
All answers are appreciated
It's a case of knowing, within the loop, when to add and when to subtract. A simple tracker will help us here.
$somme = 0;
$i = 1;
$op = 'sub';
while($i <= 100){
$somme = $op == 'sub' ? $somme - $i : $somme + $i;
$op = $op == 'sub' ? 'add' : 'sub';
$i++;
}
As noted in the comments, you could also decide the operation based on whether $i is even or odd.

Euler Project 14 - Longest Collatz sequence in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This is my solution to Euler Project Problem 14
<?php
$count = 0 ;
$max = 0;
for($n = 2 ; $n < 1000000 ; $n++){
while ($n > 1)
{
if ($n % 2 == 0 )
{
$n = $n/2;
}
else
{
$n = 3*$n + 1 ;
}
$count += 1;
if($count > $max )
{
$max = $count;
$final = $n;
}
}
}
echo $final;
>?
It took so long to run.I looked some other solutions and they were very similar to my code logically,but they were running way too faster than mine.
My question is,what is it that makes my code inefficient? What am I missing here?
Thanks ^^
Your approach is straight forward and unpolished.
You can use dynamic programming in order to improve the runtime (complexity approach).
Let's say you want to compute for 5. You will have :
5 -> 16 -> 8 -> 4 -> 2 -> 1.
But if you do that you will also have computed the value for 8 and 16.
The idea is to store the value that you have already computed in order to save work when you will need them later.
Working on the complexity of an algorithm will be the key of some problems, so better get used to it early.
An other reason why it is slow, is the choice of the language. For example try with C it will run a lot faster.

generate unqie life time random number for each number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I had a list of numbers between 1 until 100000000
I need a function to get a number and return a random number between 1 to 30 For each other number...But unique!
for example each time I call getrandomnumber('3') it always return me 25! (no other random number between 1 ...30 like rand(1,30)
You could use the given number as a seed and generate the number using the standard random number function.
function getrand($num)
{
srand($num);
return rand(1 , 30);
}
The Answer
for($i = 0;$i<100;$i++)
echo '<br>'.getrand($i);
function getrand($num)
{
$num = substr($num, -2);
$num = (int)$num;
if($num < 31) return $num;
elseif($num > 30 && $num < 61) return $num - 30;
elseif($num > 60 && $num < 91) return $num - 60;
elseif($num > 90 && $num < 101) return $num - 70;
}
1-30 repeat:
$x=0;
for($i = 0;$i<100;$i++){
$x++;
if($x>30)$x=1;
echo '<br>'.$x;
}

diviside numbers using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to create a php code to execute a list of divisions, i have tried to put a code together but being a novice, it's not quite working.
<?php
$division(6,true);
for ($i = 1; $i <= 6; $i++) {
if(($division / $i) == $result)
{echo "<p>$division &division; $i = $result</p>";}
}
?>
$division is meant to be the main number, $division will get divided by every number upto and including $division.
$i should list a string of number to from 1 to 6 in this case.
then $divide ÷ $i = $result.
I am hoping for this to print out the list as shown below.
6 ÷ 1 = 6
6 ÷ 2 = 3
6 ÷ 3 = 2
6 ÷ 4 = 1.5
6 ÷ 5 = 1.2
try this...? I'm not sure what the IF statement is trying to accomplish, though
<?php
division(6);
function division($num)
{
for ($i = 1; $i < $num; $i++)
{
$result = $num / $i;
//if(($division / $i) == $result)
//{
echo "$num / $i = $result<br/>";
//}
}
}
?>
output:
6 / 1 = 6
6 / 2 = 3
6 / 3 = 2
6 / 4 = 1.5
6 / 5 = 1.2
edit: please note that % is usually the modulus operator in programming.

Categories