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

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

?>

asked Jul 22, 2013 at 1:33

ryankryank

3952 gold
badges
6 silver badges19 bronze badges

4

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!

answered Jul 22, 2013 at 1:37

Php random number not repeat

Amal MuraliAmal
Murali

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

73.9k18 gold badges126 silver badges145 bronze badges

1

try this code

$a = range(1,36);

shuffle($a);

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

print_r($array);

answered Aug 19, 2020 at 13:24

Php random number not repeat

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

CPHPython

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:


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

Method 2:


<?php
$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:


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

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

Method 4:


<?php
$tmp=array();
while(count($tmp)<5){
$tmp[]=mt_rand(1,20);
$tmp=array_unique($tmp);
}
print_r($tmp);
?>

Method 5:


<?php
$tmp = range(1,30);
print_r(array_rand($tmp,10));
?>

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
update.

Thuộc website harveymomstudy.com

Related Posts