Using a While loop to create a script that will randomly draw numbers from 0 to 100. Until the draw of number 21. I want a draw number 21 using loop while.
The trick is to keep the loop running until the number is equal to 21.
So in the condition for the while-loop you have to check if the number is not equal to 21, then regenerate a random number. If the number is equal to 21 it will jump out of the loop and in this case display the number which is obviously 21.
$number = -1;
while ($number != 21) {
$number = rand(0, 100);
}
echo $number;
Related
I want to generate range of numbers based on to total number of users in my database
<?php
//Let's assume the total number of users is 100
foreach( range(0, 100, 20) as $number){
$ranges=$number.'-'.($number+20);
echo'<button>'.$ranges.'</button>';
}
//Result:
0-20
20-40
40-60
60-80
80-100
100-120
?>
What i needed is 0-20, 21-40, 41-60, 61-80, 81-100.
I need to stop at 100 i.e the total number of users and not up to 100-120
or if you have a better approach
So two issues to deal with:
Stop at 100. Then you just decrease the loop's stop value a bit (e.g. reduce by 1, i.e. 99)
Start at one more than the previous range's end value, except for the first range which should start at 0. You can add !!$number to achieve that. This is a boolean which is true when $number is non-zero, and will coerce to a 0 or a 1, exactly what is needed.
Code:
foreach(range(0, 100 - 1, 20) as $number) {
$ranges = ($number + !!$number) . '-' . ($number + 20);
echo '<button>' . $ranges . '</button>';
}
I want to generate 30 random numbers from 1 to 100, and avoid a number from getting generated twice. So for example, out of the 30 random numbers, the number 9 won't be generated more than once.
How can I do that?
An efficient approach could be:
You can generate a range of numbers from 1 to 100.
Random Shuffle the array.
Select first 30 values out of randomised array, using array_slice function.
It shall be random and distinct values. Try:
// Generate an array of numbers from 1 to 100
$numbers = range(1,100);
// Random shuffle the array
shuffle($numbers);
// Take first 30 values out of the array (it will be random and distinct)
$random_30 = array_slice($numbers, 0, 30);
Keep adding numbers until there are 30, each time use in_array and mt_rand to get an unused random number between 1 and 100.
$random_numbers = [];
while(count($random_numbers) < 30){
do {
$random_number = mt_rand(1,100);
} while (in_array($random_number, $random_numbers));
$random_numbers[] = $random_number;
}
var_dump($random_numbers);
If I wanted a random number between one and three I could do $n = mt_rand(1,3).
There is a 33% chance that $n = 1, a 33% chance it's 2, and a 33% chance that it's 3.
What if I want to make it more difficult to get a 3 than a 1?
Say I want a 50% chance that a 1 is drawn, a 30% chance that a 2 is drawn and a 20% chance that a 3 is drawn?
I need a scalable solution as the possible range will vary between 1-3 and 1-100, but in general I'd like the lower numbers to be drawn more often than the higher ones.
How can I accomplish this?
There is a simple explanation of how you can use standard uniform random variable to produce random variable with a distribution similar to the one you want:
https://math.stackexchange.com/a/241543
This is maths.
In your example the just chose a random number between 0 and 99.
Values returned between 0 to 49 - call it 1
Values returned between 50 - 69 - Call it 2
Values returned between 70 - 99 - Call it 3
Simple if statement will do this or populate an array for the distribution required
Assuming a 1 - 10 scale, you can use a simple if statement and have the numbers represent percentages. And just have each if statement set $n to a specific. Only downfall, it isn't universal.
$dummy = mt_rand(1,10);
// represents 50%
if ($dummy <= 5) {
$n = 1;
}
// represents 40%
if ($dummy >= 6 && $dummy <= 9) {
$n = 2;
} else {
// represents 10%
$n = 3;
}
I need to generate a random number.
But the twist is that the % of lower numbers should be greater than the higher.
For example.
rand > 1 to 100
13,
15,
12,
16,
87,
15,
27,
12,
1,
12,
98,
12,
53,
12,
14....
The bold integers will be the ones return from the 1-100 range.
The math should be like so rand = a number lower than max/2
Hope you guys can help.
Ps, How would a matrix come into this ? im not superior at maths :(
The abs answer seems to be the one.
$up = $down = 0;
while(true)
{
if(abs((rand()%150)-50) < 50)
{
$up++;
}else
{
$down++;
}
if( ($up + $down) == 500){ break;}
}
echo $up . '/' . $down;
how about
n = abs((rand()%150)-50)
$x = rand(0,1) ? rand(1,100) : rand(1,50);
Simple method: the first rand(0,1) selects between the two cases. Either 1..50 or 1..100 as random range. Since 1,100 already encompases 1,50, the latter range is selected 100% of the time, the former case only in 1 of 2 runs.
If you want a distribution where the highest numer 99 gets selected almost never, but the lower numbers 1..20 pretty frequent, then a simple rand(1,rand(1,100)) would do.
$rand = (rand() * rand()) / (getrandmax() * getrandmax());
This will give you a random number between 0 and 1 with a high probability of falling into the lower end of the range and the probability of a larger number decreasing exponentially. You can then scale this result to any range that you want.
1st number: 50
2. 30
3. 70
4. 40
5. 11
and other number is 33
I need to calculate which two numbers the last number is between (using php) .. any help?
Iterate over your list and find the following two values:
The largest number that is smaller than your target number.
The smallest number that is larger than your target number.
In pseudo-code:
lowerlimit = Unknown
upperlimit = Unknown
for each n in list:
if (n <= target) and (lowerlimit is Unknown or n > lowerlimit):
lowerlimit = n
if (n >= target) and (upperlimit is Unknown or n < upperlimit):
upperlimit = n
Then lowerlimit and upperlimit are your answer. This algorithm requires O(n) time and O(1) extra space.
If you are going to test the same list with many different target numbers then it could make sense to sort the list first requiring O(n log(n)) time, but then you can find the limits in just O(log(n)) time using a binary search.
I'll not give you the code but give you some guidelines for your homework.
You need to do these steps to solve your problem.
Sort your list of numbers. (I guess you are storing them in an array so sort the array.)
With a for loop search for the place where the element N is bigger than your number and the element N-1 is smaller. That will give you your position.
Oh and to avoid really long loop. use "break" after you find your position.
Sorted List:
11
30
// your 33 is bigger than 30 and smaller than 40, so this is the position you want.
40
50
70
function isBetween($several_numbers, $number)
{
$return_numbers = array();
sort($several_numbers);
$j = 0;
//find the first number in the array that $number is bigger than
while($number > $several_numbers[$j]) $j++;
if ($j == 0 || $j > count($several_numbers) - 1) return array();
$return_numbers[0] = $several_numbers[$j-1];
while($number > $several_numbers[$j]) $j++;
if ($j > count($several_numbers)-1) return array();
$return_numbers[1] = $several_numbers[$j];
return $return_numbers;
}
print_r(isBetween(array(50, 30, 70, 40, 10), 33));
I don't know if I understood correctly but this seems to be it