Php random number generator no duplicates

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 "

ryankryank

3952 gold badges6 silver badges19 bronze badges

4

Easiest solution:

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

Alternative:


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 generator no duplicates

Amal MuraliAmal Murali

73.8k18 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 generator no duplicates

I would try this via while loop:

";

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

  echo "
";
  print_r($arr);
  echo "
";

CPHPython

10.4k4 gold badges54 silver badges68 bronze badges

answered Sep 14, 2018 at 9:49

Php random number generator no duplicates

Not the answer you're looking for? Browse other questions tagged php algorithm api random or ask your own question.

Instantly share code, notes, and snippets.

generate random numbers in php with no repeats

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

function randomDigits($length){
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}

i got error undefined $digits variable ^^

Hi

you should to give a value to variable before loop

Like this :

function randomDigits($length){
$numbers = range(0,9);
shuffle($numbers);
$digits = "";
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}

Problem solved

$digits = '';
function randomDigits($length){
    $numbers = range(0,9);
    shuffle($numbers);
    for($i = 0; $i < $length; $i++){
    	global $digits;
       	$digits .= $numbers[$i];
    }
    return $digits;
}

`

How can i authenticated this against database values?

How can i authenticated this against database values?

You can just search the value in your database, if it returns null then it has not been stored yet.

just doing like that
function randomDigits($length){ // function with parameter you must define it before callback the function
global $digits; // global to access from anyway
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}
echo randomDigits(4); // 4 mean your number between 4 digits

How do you generate a non repeating random number in PHP?

php session_start(); if (! isset($_SESSION['numbers'])) { $_SESSION['numbers']="*"; //---create the session variable } function get_number() { $i = 0; do { $num=rand(1,20); //---generate a random number if (! strstr($_SESSION['numbers'],"*".

How do you make a random number not repeat?

Generate Random Number List With No Duplicates in Excel.
Select cell B3 and click on it..
Insert the formula: =RANDBETWEEN(10,30).
Press enter..
Drag the formula down to the other cells in the column by clicking and dragging the little “+” icon at the bottom-right of the cell..

How can I generate 5 random numbers in PHP?

rand() or mt_rand() functions can be used to Generate 5 Digit Random Number in PHP.

How do I generate a random number in PHP?

The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.