devWhiz Posted June 3, 2011 Share Posted June 3, 2011 $Variable[] = "One."; $Variable[] = "Two."; $Variable[] = "Three."; $Variable[] = "Four."; $Variable[] = "Five."; for($i=1; $i!=10000; $i++) { $Random = rand(0, count($Variable)-1); echo $Variable[$Random]."\n"; } That sometimes echos Two. Two. Five. Three. Three. What could I do to make it echo every variable but does not repeat like above... Im not sure if that would be possible or not but if so if you could point me in the right direction that would be great Quote Link to comment Share on other sites More sharing options...
Fadion Posted June 3, 2011 Share Posted June 3, 2011 I don't know for sure what you're trying to achieve, but from your description, the code below is much simpler and a lot faster. <?php $arr = array('One', 'Two', 'Three', 'Four', 'Five'); shuffle($arr); foreach ($arr as $a) { echo $a . '<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 3, 2011 Share Posted June 3, 2011 This is basically the same as GuiltyGear's code, the only difference is that it doesn't re-order the original array. $Variable[] = "One."; $Variable[] = "Two."; $Variable[] = "Three."; $Variable[] = "Four."; $Variable[] = "Five."; $rands = array_rand($Variable, count($Variable)); foreach($rands as $key){ echo $Variable[$key]."<br />"; } Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 3, 2011 Share Posted June 3, 2011 Not sure what you want, but sounds like you want to pull out random values from an array but never repeat them? try: array_splice ( $Variable , $key , 1); this will basically return the value at $variable[$key] and remove it from the array (so it can not repeat). Hope this helps Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 3, 2011 Share Posted June 3, 2011 GuiltyGear's solution is by far the simplest and most efficient.But, I have to ask... In your example you have an array of 5 values and then run a loop 1,000 times to get a random value out of that array. Is that a true example where you need more values than exist in the array. In other words, do you need to be be able to run through (randomly) every value in the array completely and then start over again until you reach some specific number or will you only need each value one time. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 3, 2011 Share Posted June 3, 2011 Assuming that you need to get go through all the values in the array multiple times but you want to go through each value before starting over, this will work for you: $arr = array('One', 'Two', 'Three', 'Four', 'Five'); $tmpArray = array(); for($i=0; $i<15; $i++) { if(count($tmpArray)<1) { $tmpArray = $arr; shuffle($tmpArray); } $value = array_shift($tmpArray); echo "{$value}<br />\n"; } Sample output: Five Three Four Two One Three Four Two Five One Five One Three Four Two Quote Link to comment 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.