Generating random numbers without repeats

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!

Leave a Comment