Jacks89 Posted August 21, 2009 Share Posted August 21, 2009 I need advice in generating a set of random number without it repeating. I got the result I desire with my code but I am wondering if there is an easy method of doing it. This is my code <?php $row = 1; while( $row<=100){ echo "<tr><td>".$row."</td>"; $a= rand(1,45); $b= rand(1,45); $c= rand(1,45); $d= rand(1,45); $e= rand(1,45); $f= rand(1,45); $g= rand(1,45); $h= rand(1,45); $i= rand(1,45); $j= rand(1,45); $k= rand(1,45); $l= rand(1,45); //for b while ($b==$a){ $b=rand(1,45); $b!=$a; } //for c while ($c==$a OR $c==$b){ $c=rand(1,45); $c!=$a; $c!=$b; } //for d while ($d==$a OR $d==$b OR $d==$c){ $d=rand(1,45); $d!=$a; $d!=$b; $d!=$c; } //for e while ($e==$a OR $e==$b OR $e==$c OR $e==$d){ $e=rand(1,45); $e!=$a; $e!=$b; $e!=$c; $e!=$d; } //for f while ($f==$a OR $f==$b OR $f==$c OR $f==$d OR $f==$e){ $f=rand(1,45); $f!=$a; $f!=$b; $f!=$c; $f!=$d; $f!=$e; } //for g while ($g==$a OR $g==$b OR $g==$c OR $g==$d OR $g==$e OR $g==$f){ $g=rand(1,45); $g!=$a; $g!=$b; $g!=$c; $g!=$d; $g!=$e; $g!=$f; } //for h while ($h==$a OR $h==$b OR $h==$c OR $h==$d OR $h==$e OR $h==$f OR $h==$g){ $h=rand(1,45); $h!=$a; $h!=$b; $h!=$c; $h!=$d; $h!=$e; $h!=$f; $h!=$g; } //for i while ($i==$a OR $i==$b OR $i==$c OR $i==$d OR $i==$e OR $i==$f OR $i==$g OR $i==$h){ $i=rand(1,45); $i!=$a; $i!=$b; $i!=$c; $i!=$d; $i!=$e; $i!=$f; $i!=$g; $i!=$h; } //for j while ($j==$a OR $j==$b OR $j==$c OR $j==$d OR $j==$e OR $j==$f OR $j==$g OR $j==$h OR $j==$i){ $j=rand(1,45); $j!=$a; $j!=$b; $j!=$c; $j!=$d; $j!=$e; $j!=$f; $j!=$g; $j!=$h; $j!=$i; } //for k while ($k==$a OR $k==$b OR $k==$c OR $k==$d OR $k==$e OR $k==$f OR $k==$g OR $k==$h OR $k==$i OR $k==$j){ $k=rand(1,45); $k!=$a; $k!=$b; $k!=$c; $k!=$d; $k!=$e; $k!=$f; $k!=$g; $k!=$h; $k!=$i; $k!=$j; } //for l while ($l==$a OR $l==$b OR $l==$c OR $l==$d OR $l==$e OR $l==$f OR $l==$g OR $l==$h OR $l==$i OR $l==$j OR $l==$k){ $l=rand(1,45); $l!=$a; $l!=$b; $l!=$c; $l!=$d; $l!=$e; $l!=$f; $l!=$g; $l!=$h; $l!=$i; $l!=$j; $l!=$k; } $no= array( $a, $b , $c, $d, $e, $f , $g, $h, $i, $j, $k, $l ); sort($no); foreach ($no as $key => $val) { echo "<td>".$val."</td> "; } echo "</tr>"; $row++; } ?> The current code that I have actually do not have much problem for generating 100 set of number of 12.. But the more I generate (usually up to 4 time), my browsers start to get very slow. Link to comment https://forums.phpfreaks.com/topic/171244-generate-a-series-of-random-no/ Share on other sites More sharing options...
Daniel0 Posted August 21, 2009 Share Posted August 21, 2009 Like this? $numbers = array(); for ($i = 0; $i < 12; ++$i) { $number = mt_rand(1, 45); // mt_rand() is approximately four times faster than rand() if (!in_array($number, $numbers)) { $numbers[] = $number; } else { --$i; } } Link to comment https://forums.phpfreaks.com/topic/171244-generate-a-series-of-random-no/#findComment-903098 Share on other sites More sharing options...
Mark Baker Posted August 21, 2009 Share Posted August 21, 2009 $possibleValues = range(1,45); shuffle($possibleValues); $actualValues = array_slice($possibleValues,0,12); Link to comment https://forums.phpfreaks.com/topic/171244-generate-a-series-of-random-no/#findComment-903192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.