I need to create a random 15 character string without duplicate characters using php, these characters are numbers only ranging from 0 - 90 for example my the generated string would look like: 10, 44, 88, 1, 30 and so on. What would be the best way to do this?
Asked
Active
Viewed 971 times
-2
-
1So... As I understand it, you want to create a 15 characters long string, and none of them must have a duplicate, and you intend to do this, with 10 different numbers? – Epodax Apr 22 '15 at 08:21
-
please follow some php tutorial – Dev Apr 22 '15 at 08:22
-
possible duplicate of [PHP random string generator](http://stackoverflow.com/questions/4356289/php-random-string-generator) – Ahmed Ziani Apr 22 '15 at 08:23
-
Thats correct the numbers i want in my string must be between 0 and 90 and the string itself must be 15 characters long without duplicate numbers. – MegaDallion2 Apr 22 '15 at 08:27
-
@MegaDallion2 Isn't it an option to random 15 times, check if the number is the same as one of the previous ones and then get them all together seperated by a comma? – Loko Apr 22 '15 at 09:01
-
the commas are just to seperate the numbers in my example not part of the string sorry – MegaDallion2 Apr 22 '15 at 09:12
1 Answers
0
I've got this now:
$string='';
for($i=0;$i<15; $i++){
$number[$i]=mt_rand(0, 90);
if(in_array($number[$i],$number)){
//Randomned number already exists
}else{
//Randomned number doesn't exist yet
}
$string.=$number[$i].',';
}
var_dump($string);
You should check if it exists yet or not yourself. Just for educational purposes, I'm using: mt_rand();
and in_array();
. mt_rand(x,y);
provides a random number between x and y. in_array(x,y);
checks if the value(x) exists in the array(y).

Loko
- 6,539
- 14
- 50
- 78