kryppienation Posted July 22, 2009 Share Posted July 22, 2009 I am trying to draw 5 random numbers between 1 and 99, the problem I'm having is trying to make sure all the numbers are different. I am wondering if someone can help me actually have this check to make sure that all 5 numbers are different or even if there is some better way for me to accomplish this. <?php $C = 0; while($C <= 4){ $drawing = rand(1, 99); echo ''.$drawing.'.'; $C++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/166944-solved-drawing-numbers/ Share on other sites More sharing options...
jayjay960 Posted July 22, 2009 Share Posted July 22, 2009 <?php $C = 0; while($C <= 4){ $drawing = rand(1, 99); $drawings[$C] = $drawing; for ($n = 0; $n <= $C; $n++) { if ($drawing==$drawings[$n]) { while ($drawing==$drawings[$n]) { $drawing = rand(1, 99); $drawings[$C] = $drawing; } } } echo ''.$drawing.'.'; $C++; } ?> There, I think that should work Quote Link to comment https://forums.phpfreaks.com/topic/166944-solved-drawing-numbers/#findComment-880210 Share on other sites More sharing options...
kryppienation Posted July 22, 2009 Author Share Posted July 22, 2009 i tried that just now and i got a endless loop with nothing showing up at all as output. Quote Link to comment https://forums.phpfreaks.com/topic/166944-solved-drawing-numbers/#findComment-880214 Share on other sites More sharing options...
Mark Baker Posted July 22, 2009 Share Posted July 22, 2009 $numberset = range(1,99); shuffle($numberset); for ($i = 1; $i <= 5; $i++) { echo array_pop($numberset).'<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/166944-solved-drawing-numbers/#findComment-880269 Share on other sites More sharing options...
kryppienation Posted July 22, 2009 Author Share Posted July 22, 2009 $numberset = range(1,99); shuffle($numberset); for ($i = 1; $i <= 5; $i++) { echo array_pop($numberset).'<br />'; } Thank you very much this seems to work excellent, I'm wondering tho, is there anyway that i can assign a variable to each number? I would really like to be able to work with these 5 numbers as separate variables so that they can be called upon later in the code. Quote Link to comment https://forums.phpfreaks.com/topic/166944-solved-drawing-numbers/#findComment-880496 Share on other sites More sharing options...
akitchin Posted July 22, 2009 Share Posted July 22, 2009 that's simply a matter of assigning the array_pop() expression to a variable instead of echoing it: <?php $numberset = range(1,99); shuffle($numberset); $numbers = array(); for ($i = 1; $i <= 5; $i++) { $numbers[] = array_pop($numberset); } echo '<pre>'.print_r($numbers, TRUE).'</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/166944-solved-drawing-numbers/#findComment-880502 Share on other sites More sharing options...
Mark Baker Posted July 22, 2009 Share Posted July 22, 2009 Thank you very much this seems to work excellent, I'm wondering tho, is there anyway that i can assign a variable to each number? I would really like to be able to work with these 5 numbers as separate variables so that they can be called upon later in the code. $numberset = range(1,99); shuffle($numberset); for ($i = 1; $i <= 5; $i++) { $var = 'number'.$i; $$var = array_pop($numberset); } echo $number1.' '.$number2.' '.$number3.' '.$number4.' '.$number5.'<br />'; But you'd be better assigning the values into an array $numberset = range(1,99); shuffle($numberset); $numbers = array_slice($numberset,0,5); echo $numbers[0].' '.$numbers[1].' '.$numbers[2].' '.$numbers[3].' '.$numbers[4].'<br />'; Quote Link to comment https://forums.phpfreaks.com/topic/166944-solved-drawing-numbers/#findComment-880504 Share on other sites More sharing options...
kryppienation Posted July 22, 2009 Author Share Posted July 22, 2009 Thanks so much for your help guys, this will be perfect for what I'm trying to do. I appreciate your time spent in helping me figure this out!! Quote Link to comment https://forums.phpfreaks.com/topic/166944-solved-drawing-numbers/#findComment-880508 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.