Hello everyone!
I need to generate two numbers that are NOT equal in PHP.
I know that I use $random1 = (rand()%9);
to generate random a number between 0-9. I need to add something to this I guess.
Thanks!
Hello everyone!
I need to generate two numbers that are NOT equal in PHP.
I know that I use $random1 = (rand()%9);
to generate random a number between 0-9. I need to add something to this I guess.
Thanks!
If you simply want unique numbers, just start at zero and add +1 for every new number.
If you need random unique numbers, just save your previous numbers in a list and when you need a new one, just generate random numbers until you found one, which is not in that list.
If you need unique identifiers, you can use the built-in function uniqid
.
$random1 = (rand() % 10)
$random2 = $random1 + (rand()%9)+1
In this way they are never equal(as at least 1 will be added to $random1 )
if both number have to be from 0-9, you just have to do it with a last mod-operation:
$random1 = (rand() % 10)
$random2 = ($random1 + (rand()%9)+1 ) %10
Hi
Interpreting your question rather heavily it looks like you want to pseudo-randomly generate 2 different integers between 0 and 9 inclusive ?
So generate the first, generate another, if it's the same as the first repeat until it isn't.
I assert, without justification, that this will be as efficient as any other method.
Regards
Mark
I think you'll find that rand()%9
will give you a number in the range 0 through 8 inclusive. If you want 0 through 9 inclusive, you should use rand()%10
. I'm going to assume that's what you wanted but you could adjust the answer easily if 0 through 8 was what you really intended.
If you want two numbers in that range, the easiest way is to generate one in that range then generate another in one less than that range and, if it's identical or greater, increment it.
This is the mathematical way to do it, and it's deterministic (always two calls to rand()
no matter what). Although unlikely, a true random number generator could produce a string of numbers all identical which would make the looping solutions unwise (you're likely to be using a linear generator so this probably won't be a problem).
On the first attempt, you have the full range to choose from (10 numbers, 0 through 9). On the second you have the full range minus the number already chosen. So, if your first number was 7, you generate a number from 0 through 8 and map 7 to 8 and 8 to 9:
$random1 = (rand() % 10);
$random2 = (rand() % 9);
if $random2 >= $random1 {
$random2 = $random2 + 1;
}
$r = str_shuffle("0123456789");
$r1 = (int) $r[0];
$r2 = (int) $r[1];
function random($min, $max) {
$stack = range($min, $max);
shuffle($stack);
$nr1 = array_pop($stack);
$nr2 = array_pop($stack);
return array($nr1, $nr2);
}
This might do the trick without being a math wiz :)
$numbers = array_rand(range(0, 9), 2);
echo '<pre>';
print_r($numbers);
echo '</pre>';
Alix Axel's solution was the most straightforward and worked fine for me. I wanted three random entries from an existing array, so I used
$rands = array_rand(range(0,count($otherarray)),3);
And then displayed entries for which the key was in $rands
.