Php random number not repeat

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);


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


asked Jul 22, 2013 at 1:33


3952 gold
6 silver badges19 bronze badges


Easiest solution:

$numbers = range(1, 20);



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

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


Similar question: #5612656



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!

answered Jul 22, 2013 at 1:37

Php random number not repeat

Amal MuraliAmal

Xem thêm:  Sau sinh bao lâu thì mẹ được cắt móng tay

73.9k18 gold badges126 silver badges145 bronze badges


try this code

$a = range(1,36);


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


answered Aug 19, 2020 at 13:24

Php random number not repeat

I would try this via while loop:


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

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

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

  echo "<pre>";
  echo "</pre>";


10.5k4 gold badges54 silver badges68 bronze badges

answered Sep 14, 2018 at 9:49

Php random number not repeat

Whether it is Web application, WAP or mobile application, random numbers have their applications. In the recent contact with several small projects, I often need to deal with random numbers or random arrays, so for PHP how to generate non-repeating random numbers commonly used in a summary of several methods (ps: methods 1, 4 and 5 are commonly used by me, and the rest come from network collation)

Method 1:

$numbers = range (1,50);
//shuffle Scramble the array order immediately
shuffle ($numbers);
//array_slice Fetches a certain in the array 1 Segment
$result = array_slice($numbers,0,$num);

Method 2:

$numbers = range (1,20);
// Sow the seeds of random number generator, optional, and have no effect on the results after testing
srand ((float)microtime()*1000000);
shuffle ($numbers);
// Skip list No. 1 1 Values (the index is saved)
while (list(, $number) = each ($numbers)) {
echo "$number ";

Method 3:

function NoRand($begin=0,$end=20,$limit=5){
shuffle($rand_array);// Call the ready-made array random arrangement function
return array_slice($rand_array,0,$limit);// Before interception $limit A

above can randomly generate 5 non-repeating values between 1 and 20

Method 4:


Method 5:

$tmp = range(1,30);

This may be simpler than it is called (ps: If you specify a step size in range, you must pay attention to whether the second parameter of array_rand exceeds the length of $tmp).

PHP provides a very rich array function, most random numbers can be generated from the perspective of arrays, if you have methods to provide, welcome to give, the article will continue to

Thuộc website

Related Posts