Generate two numbers that are NOT equal in PHP
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 some开发者_如何学Cthing to this I guess.
Thanks!
$r = str_shuffle("0123456789");
$r1 = (int) $r[0];
$r2 = (int) $r[1];
$rnd1 = rand(0,9);
do {
$rnd2 = rand(0,9);
} while ($rnd1 == $rnd2);
$random1 = 4;
$random2 = 5;
$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
$numbers = array_rand(range(0, 9), 2);
echo '<pre>';
print_r($numbers);
echo '</pre>';
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 :)
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;
}
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
.
$r = mt_rand(0, 9);
$r2 = ($r || $r === 9) ? $r-1 : $r+1;
Explain:
$r
: it's number from 0 to 9.
$r2
: it's allways from 0 to 9 but $r-1
or $r+1
More randomly way is generate 2 numbers:
$r = mt_rand(0, 9);
$r2 = mt_rand(0, 9);
$r2 = $r!==$r2 ? $r2 : (($r || $r === 9) ? $r-1 : $r+1);
Also if you using PHP 7+ you can change mt_rand to random_int
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
.
$random1 = rand() % 9;
$random2 = ($random1 + 1) % 9;
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.
$random_numbers = array();
for ($i = 0; $i <= 1; $i++)
{
$random_numbers[$i] = rand(1,13);
}
I solved it by putting the rand
inside an array inside a for loop. This way I got two different random numbers! The two numbers are placed in the array in two fields, random_numbers[0]
and random_numbers[1]
精彩评论