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