Ryan
Ryan

Reputation: 2234

php random number within range - no repeat

Haven't been able to find what I'm looking for so far, so I'm asking for help.

I have a div with a class that needs to end in a number between 1 and 8, and I need it to be a random number, without repeating...

<div class="parallax parallax_<?php echo $random_number ?>"></div>

I figured this should be pretty simple, but I'm having trouble.

Currently, I have:

<div class="parallax parallax_<?php echo rand(1, 8); ?>"></div>

which works, but produces duplicates.

EDIT

So after testing, I realized I am running into a problem. I am using this within a wordpress template. I am querying a set of 6 posts, and for each set of posts, I am including the above parallax div. So, I get a random, none repeating number for each query, but each query resets the numbers - giving me duplicates... Here is my entire code.

<?php
  $args = array(
    'post_type'        => 'post',
    'orderby'          => 'menu_order',
    'posts_per_page'   => -1,
    'order'            => 'ASC'
  );
  $posts = get_posts( $args );
?>

<?php foreach (array_chunk($posts, 6, true) as $posts) :  ?>
  <div class="parallax parallax_<?php echo rand(1, 8); ?>"></div>
  <div class="posts_container">  
    <?php foreach( $posts as $post ) : setup_postdata($post); ?>

      <div class="post">
        <div class="post__thumbnail"><a href="<?php the_permalink();?>"><?php the_post_thumbnail(); ?></a></div>
        <div class="post__title"><?php the_title(); ?></div>
      </div>

    <?php endforeach; ?> 
  </div>
<?php endforeach; ?>

Upvotes: 2

Views: 8360

Answers (4)

Ionut Necula
Ionut Necula

Reputation: 11472

You can use uniqid() function along with md5 to strengthen the results:

 md5(uniqid(rand(1, 8), true));

Or use shuffle.

$numbers = range(1, 8);
shuffle($numbers);

foreach ($numbers as $number) {
    echo "$number ";
}

Upvotes: 0

Andrej Buday
Andrej Buday

Reputation: 609

This code prevent duplicate and fits to any other test case scenario. Shuffling is not necessary, random element is based on array_rand() function.

To pick repetitively new random key it is necessary to use code in the loop.

// Array declaration
$a=array(1,2,3,4,5,6,7,8);

// Loop
$randomKey=array_rand($a);
unset($a[$randomKey]);

// Test
echo $randomKey."<br>";
var_dump($a);

First it is defined an array of numbers which are desired. Than random key from array is picked. It is also unset from array so it will be not used again.

For repeating action $randomKey and unset() should be in loop. Echo and var_dump() function is only for testing purpose. Array size can decrease unsetting already used key or stay same length leaving unset() function from the loop.

Upvotes: 4

Thamaraiselvam
Thamaraiselvam

Reputation: 7080

I think this could help you. shuffle() here

<?php
$numbers = range(1, 8);
shuffle($numbers);
foreach ($numbers as $number) {
    echo "$number ";
}

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

I would do a range and shuffle:

$myRange = range(1, 8);
shuffle($myRange);
return $myRange[0];

Upvotes: 5

Related Questions