Generating random numbers without repeats
Asked Answered
B

4

18

I'm building a website that will randomly display a yelp listing each time the page is refreshed. The yelp search api returns 20 listings in an array. Right now, I am using PHP's function rand(0,19) to generate a random listing every time the page is refreshed ( $businesses[rand(0,19)] ).

Can someone refer me to a smarter method to randomize? I want to show all 20 listings once before any of them are repeated. What is the preferred method to handle this problem?

the below answer doesn't work because the numbers are recreated every time I refresh the page. I'm guessing I need to store which numbers I've used already?

$numbers = range(0, 19);

shuffle($numbers);

// Handle Yelp response data
$response = json_decode($data);
$RANDOM = rand(1,19);
$business = $response->businesses;

echo "<img border=0 src='".$business[$RANDOM]->image_url."'><br/>";
echo $business[$RANDOM]->name."<br/>";
echo "<img border=0 src='".$business[$RANDOM]->rating_img_url_large."'><br/>";

?>
Birdella answered 22/7, 2013 at 1:33 Comment(4)
The easiest way to do this is pre-populate a list of sanitized random values before hand. Why wouldn't you just create the order, in advance, and pick the last one that hadn't been displayed. To casual users, this will appear to be random.Strickland
how do you keep all 20 around to show them? do you store the listings locally, or does the api send you the same ones on request?Saenz
Look up 'Fisher Yates', or see #8117372.Parrotfish
@Saenz I currently am not storing any listings. The api sends 20 listings in an array called $businesses. I want to display one listing at a time per refreshBirdella
F
41

Easiest solution:

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

Alternative:

<?php

function randomGen($min, $max, $quantity) {
    $numbers = range($min, $max);
    shuffle($numbers);
    return array_slice($numbers, 0, $quantity);
}

print_r(randomGen(0,20,20)); //generates 20 unique random numbers

?>

Similar question: #5612656

Codepad: http://codepad.org/cBaGHxFU

Update:

You're getting all the listings in an array called $businesses.

  1. Generate a random listing ID using the method given above, and then store it your database table.
  2. On each page refresh, generate a random listing ID, and check if it matches the value in your database. If not, display that listing and add that value to your table.
  3. Go to step 1.

When this is completed, you will have displayed all the 20 listings at once.

Hope this helps!

Familiarity answered 22/7, 2013 at 1:37 Comment(1)
Sorry, I should have included code for better clarification. Please look at my EditBirdella
D
2

try this code

$a = range(1,36);

shuffle($a);

$array = array_slice($a, 0, 3);

print_r($array);
Delainedelainey answered 19/8, 2020 at 13:24 Comment(0)
D
0

You can use microtime and rand together

echo rand(11111,99999).substr(round(microtime(true) * 1000),6);
Disavow answered 31/7 at 3:40 Comment(0)
E
-2

I would try this via while loop:

<?php

  $i = 0;
  $arr = array();

  while($i<10){
    $num = rand(1, 12);
    $c = 0;
    echo $num . "<br>";

    for($j=0; $j<4; $j++){
      if($arr[$j] == $num){
        $c++;
        break;
      }
    }
    if($c==0){
      $arr[$i] = $num;
      $i++;
    }
  }

  echo "<pre>";
  print_r($arr);
  echo "</pre>";
Escargot answered 14/9, 2018 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.