PHP random number generator ff
I am writing a random number generator in php (have no idea what i am going to do with it yet haha), but i want other people to be able to use it too, can someone please tell me if the following will be a problem ,because of the way it generates numbers the maximum for the number is currently set in the following order, 10 ,100,1000,10000,100000,1000000 and so on, will this be a problem ? Should the maximum be more precise ? when i set it to 1100, the maximum is still 1000, should i spend time to make it precise ? or is it fine as is ? please any input is appreciated.
11 years ago
0
Yes it is strange, do you solve your problem? You may can multiply your max by 1.1 but I think I don’t really understand your problem…
how about using a built in function???
rand(lowest number,highest number);
so basicly
<?php
// if its between 0 and 1854524
$random_number = rand(0,1854528);
// do what ever you want with your random number
?>
if you want a set of random numbers that you do not want replectaed
it can be done this way
<?php
$randoms = array();
// if its between 0 and 1854524
$random_number = rand(0,1854528);
//if you want a set of 10
for($looper = 0;$looper<10;$looper++)
{
if(!in_array($random_number,$randoms))
{
$randoms[] = $random_number;
}
}
?>
I Hate Signatures.
@Pinkponyprincess: The problem may come from the digits, right? I just can’t figure out how your function worked and made 1000 become the maximum of 1100.
[quote=ADIGA]if you want a set of random numbers that you do not want replectaed
it can be done this way[…][/quote]
1) This way will just put one number in the array as $random_number is computed only once.
2) Even if the rand number was recomputed, this won’t also enforce 10 values but a range between 1 and 10
I guess it wasn’t your purpose !
Here is a possible way to do:
$randoms = array();
// If you want a set of 10
$maxNumber = 10;
// Define min and max rand's values for granularity
$minRand = 0;
$maxRand = 1854528;
while ($maxNumber > 0)
{
// Recompute the random number
$random_number = rand($minRand, $maxRand);
if (!in_array($random_number, $randoms))
{
$randoms[] = $random_number;
// One number has been set, we can decrease the amount of value we need.
--$maxNumber;
}
}
Note:
- the “while” part should be embed in a function and by then be reusable else where ~
- “rand” is actually an existing function in php, don’t forget to update it by yours ~
Thanks Memoria, wrong location of rand function.
```<?php
$randoms = array();
// if its between 0 and 1854524
//if you want a set of 10
for($looper = 0;$looper<10;$looper++)
{
$random_number = rand(0,1854528); //<—– :P
if(!in_array($random_number,$randoms))
{
$randoms[] = $random_number;
}
}
?>```
I Hate Signatures.
```<?php
$randoms = array();
// if its between 0 and 1854524
//if you want a set of 10
for($looper = 0;$looper<10;$looper++)
{
$random_number = rand(0,1854528); //<—– :P
if(!in_array($random_number,$randoms))
{
$randoms[] = $random_number;
}
}
print_r($randoms);
?>```
try running it and see what you get :)
I Hate Signatures.
output samples
Array ( [0] => 1601479 [1] => 534454 [2] => 426594 [3] => 1149070 [4] => 903623 [5] => 254567 [6] => 703236 [7] => 953637 [8] => 1443417 [9] => 382292 )
Array ( [0] => 112306 [1] => 1478919 [2] => 397178 [3] => 1018657 [4] => 1050485 [5] => 869085 [6] => 91333 [7] => 1142748 [8] => 653334 [9] => 1578977 )
Array ( [0] => 117888 [1] => 1726791 [2] => 1470937 [3] => 73707 [4] => 1476876 [5] => 652197 [6] => 1415231 [7] => 78266 [8] => 630534 [9] => 743751 )
I Hate Signatures.
Your code is “working” only because the max rand you’re using is set high enough to not get this error coming out.
Just set it to 10 for example and you won’t have an array of 10 values :).