In my application i need to generate random numbers that should contain certain character like capital letter, a number and of certain length. It will be an honor if you guys help me out.
Asked
Active
Viewed 105 times
-3
-
1Generate arrays/lists of characters that you want to require (one for lower, one for upper, one for numbers...etc). Pick however many characters you want from each list and join them together. – Jonathan Kuhn Jan 08 '16 at 18:37
-
You should not just simply join them, though. You should a) randomize the number of characters you take from each list (ensuring the required minimum amount from each list), then *shuffle* the results together so that a hacker doesn't know you first have the lower case, then the upper case, then the numbers. – Eric J. Jan 08 '16 at 18:40
-
post what have you tried please – Heshan Sandeepa Jan 08 '16 at 18:48
2 Answers
0
I have a script that requires the word "Lock" followed by a 2 digit number followed by a random four-letter word. This is an abbreviation of what I used...
$words = array("Pick","Hard","Gene","Boat","Pass");
// Make that list as long as you like...
function get_random_string()
{
global $words;
shuffle($words);
return 'Lock'.rand(10,99).$words[0];
}

kainaw
- 4,256
- 1
- 18
- 38
0
function rand_string( $length ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return substr(str_shuffle($chars),0,$length);
}
echo rand_string(8);

Santosh Patel
- 549
- 2
- 13