Random number php no 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 badges6 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

Random number php no repeat

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

Random number php no 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.4k4 gold badges54 silver badges68 bronze badges

answered Sep 14, 2018 at 9:49

Random number php no repeat

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 to generate random number without repetition in PHP?

<? 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 ?>

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

What is rand() in PHP?

Definition and Usage. 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.

How to make random string in PHP?

Generate a random number using rand() function. Hash it using one of the above functions..
Store all the possible letters into a string..
Generate random index from 0 to string length-1..
Print the letter at that index..
Perform this step n times (where n is the length of string required)..