How to generate random number without repetition in php?

I’m going to show you 3 methods to generate random numbers without duplicates using PHP.  For this example, we generate 5 random numbers between 1 to 10.

Method 1:
We simply generate a random number using “rand()” function and check whether there are duplicates using the double loops. (Lines 4,5).  Variable $i keeps check of current index/element and variable $j represents “previous items”.

Method 2:
We create a new helper function called “contain(arg1, arg2)”.  Where arg1 = an array of number, arg2 = a number.
“contain” function checks whether the number is include in the array.  If yes, return true, o/w false.  The difference from the previous method is that this function replace the variable $j from Method 1.  In my opinion, this method is a lot clearer.  The way of thinking would be more useful when conquering harder problems in the future and thus avoiding spaghetti code in the long-run.

Method 3:
I must say this is the easiest/laziest way to implement random numbers without duplicates.
1. we use PHP’s range() function to create an array of numbers from 1 to 10.
2. Use PHP’s shuffle() function to shuffle the orders.
3. We take the first 5 elements from the array using the for loop below.
4. ET VOILA!  We now have a 5 randomly generating numbers between 1 to 10 without duplicates!!


All the above methods work perfectly. Choose the one you prefer the most ^^

About Victor

Software Developer/Tester

This entry was posted in PHP & MySQL. Bookmark the permalink.

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 do I randomize a 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.

How do you generate random numbers without repetition in flutter?

int random1 = randomPicker. removeLast(); int random2 = randomPicker. removeLast(); assert(random1 != random2);