eugeniu Posted July 18, 2008 Share Posted July 18, 2008 Let's say I want to generate 4 different random numbers from 1 to 100. How do I do that? I only know how to generate one random number... Link to comment https://forums.phpfreaks.com/topic/115495-how-do-i-generate-4-different-random-numbers/ Share on other sites More sharing options...
papaface Posted July 18, 2008 Share Posted July 18, 2008 I'd do: <?php $i = 0; while ($i < 4) { echo rand(0,100) . "<br />"; $i++; } ?> or: $rand = array(rand(0,100),rand(0,100),rand(0,100),rand(0,100)); foreach ($rand as $val) { echo $val . "<br />"; } or: $rand1 = rand(0,100); $rand2 = rand(0,100); $rand3 = rand(0,100); $rand4 = rand(0,100); echo $rand1 ." ". $rand2 ." ". $rand3 ." ". $rand4; Link to comment https://forums.phpfreaks.com/topic/115495-how-do-i-generate-4-different-random-numbers/#findComment-593749 Share on other sites More sharing options...
kenrbnsn Posted July 18, 2008 Share Posted July 18, 2008 Here's one way: <?php $ary = range(1,100); shuffle($ary); $nums = array_slice($ary,0,4); echo '<pre>' . print_r($nums,true) . '</pre>'; ?> papaface: There's no guarantee that you will get 4 different numbers. Ken Link to comment https://forums.phpfreaks.com/topic/115495-how-do-i-generate-4-different-random-numbers/#findComment-593751 Share on other sites More sharing options...
GingerRobot Posted July 18, 2008 Share Posted July 18, 2008 papaface: what guarantees that those random numbers are different? Link to comment https://forums.phpfreaks.com/topic/115495-how-do-i-generate-4-different-random-numbers/#findComment-593752 Share on other sites More sharing options...
papaface Posted July 18, 2008 Share Posted July 18, 2008 papaface: what guarantees that those random numbers are different? Nothing, I suppose lol. Link to comment https://forums.phpfreaks.com/topic/115495-how-do-i-generate-4-different-random-numbers/#findComment-593755 Share on other sites More sharing options...
eugeniu Posted July 18, 2008 Author Share Posted July 18, 2008 Here's one way: <?php $ary = range(1,100); shuffle($ary); $nums = array_slice($ary,0,4); echo '<pre>' . print_r($nums,true) . '</pre>'; ?> papaface: There's no guarantee that you will get 4 different numbers. Ken Oo, thanks. Is there any way I can make each generated number a separate variable with that? Link to comment https://forums.phpfreaks.com/topic/115495-how-do-i-generate-4-different-random-numbers/#findComment-593783 Share on other sites More sharing options...
kenrbnsn Posted July 18, 2008 Share Posted July 18, 2008 <?php $ary = range(1,100); shuffle($ary); list($num1,$num2,$num3,$num4) = array_slice($ary,0,4); ?> Or you could just reference each array item. Ken Link to comment https://forums.phpfreaks.com/topic/115495-how-do-i-generate-4-different-random-numbers/#findComment-593784 Share on other sites More sharing options...
eugeniu Posted July 18, 2008 Author Share Posted July 18, 2008 Oh, thanks Ken! Link to comment https://forums.phpfreaks.com/topic/115495-how-do-i-generate-4-different-random-numbers/#findComment-593840 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.