【发布时间】:2015-05-23 10:18:45
【问题描述】:
我做了一些研究,但没有为我的问题找到任何解决方案。
我要归档的内容:
从 0 ($min) 和 1 ($max) 中生成一个随机数,但随机数中有固定数量 ($many) 的 1。随机数的长度应为 6,如我的 while 循环 (while($xLoop <= 6))。
这是我当前的代码:
$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++;
}
它将循环 6 次,所以我们有一个长度为 6 的随机数,但我希望我的随机数中有一个固定数量的 1,即$many(这里是 3)。其余部分用 0 填充,直到我们达到长度 6。
如何修复此代码?还是有更简单的方法?
【问题讨论】:
-
我想你得把你的问题说得够清楚,这样大家才能理解和帮助你。
标签: php loops random while-loop numbers