I did some research, but didn't found any solution for my question.
What I want to archive:
Generate a random number out of 0's ($min
) and 1's ($max
), but with a fixed amount ($many
) of 1's in the random number. The random number should have a length of 6 as in my while loop (while($xLoop <= 6)
).
Here is my current code:
$min = 0;
$max = 1;
$many = 3;
$xLoop = 1;
while($xLoop <= 6) {
$nRand = mt_rand($min,$max);
if($nRand == 1){ //if random number comes out number 1
$many--; // Prevent number 1 more then $many...
//Do something...
}else{ //if random number comes out not number 1
//Do something and still looping until get 6 times
}
echo $nRand.' - '.$many.'</br>'; //For debugin... i want to see how many number 1 comes out.
$xLoop++;
}
It will loop 6 times, so we have a random number of the length 6, but I want a fixed amount of 1's in my random number, which is $many
(here 3). And the rest filled with 0's until we reach the length 6.
How can I fix this code? Or is there a simpler way?
Aucun commentaire:
Enregistrer un commentaire