Jump to content

Stop rand() from repeating


devWhiz

Recommended Posts


$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

Link to comment
Share on other sites

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 />';	
}
?>

Link to comment
Share on other sites

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 />";
}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.