2

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; ?>
5
  • 4
    random number, without repeating is not strictly a randon number Commented Oct 5, 2016 at 13:08
  • How many of these do you put on the one page Commented Oct 5, 2016 at 13:09
  • @RiggsFolly multiple. depends on a post count. Commented Oct 5, 2016 at 13:18
  • Possible duplicate of Generating random numbers without repeats Commented Oct 5, 2016 at 13:21
  • 1
    I think that EDIT is a big enough change to the question to warant you asking it as a new question Commented Oct 5, 2016 at 13:48

4 Answers 4

5

I would do a range and shuffle:

$myRange = range(1, 8);
shuffle($myRange);
return $myRange[0];
Sign up to request clarification or add additional context in comments.

11 Comments

Would be better to make the array static and pop one value off of it at a time. But using shuffle() is elegant and terse.
@RiggsFolly Didn't expect this from you. Just loop $myRange, which is what I intended.
I did say that I may have misunderstood how to run in 8 times correctly
@PraveenKumar Google translate does not make that good a job of it I am afraid, but I did guess Tamil
@RiggsFolly Google Translate is still a baby in terms of South Indian languages.
|
4

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.

2 Comments

This looks good but should echo $a[$randomKey]; and move the unset to after you echo the number!!!!!
Order doesn't matter. You can do as you please, test part is only for informative purpose.
0

I think this could help you. shuffle() here

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

Comments

0

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

3 Comments

Does not prevent duplicates.
And uniqid() and md5() do not make the result more random.
Maybe, but it's a start. Also in the loop he can add a counter to the end of each number to make it even more unique. Please do post a better answer. I dare you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.