Solved

Random password generation

Posted on 1998-12-13
3
159 Views
Last Modified: 2013-12-25
How do I generate a random password with 6 letters that vary in upper and lower case? I tried this code:

for ($numdigits = 1; $numdigits <= 6; $numdigits++) {
  srand(time ^ $$);
  @letters = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
  $randnumber = rand(@letters);
  $randpassword .= $letters[$randnumber]
}

Since the seed is the time, it just generates the same random letter six times. Also, there must be a better way to do it than having an array with all of the letters...
0
Comment
Question by:chrisbolt
3 Comments
 

Accepted Solution

by:
Gunnar earned 50 total points
ID: 1830102
You should not call srand for each iteration. Call it only once, when your program starts. If you use Perl 5.004 or later, it is not necessary to call srand() at all, as it is done automatically. (Actually, it was done automatically in older versions too, but with a bad seed.)

To avoid the letter array, generate an ASCII code between 65 and 80 (A..Z). Then, flip a coin (that is, check if rand() > 0.5); if so, add 32 (which transfers the code to the range 97..112, a..z). Example:

srand(time ^ $$);

for ($n = 1; $n <= 6; $n++) {
  $ch = 65+rand(26);
  if (rand() > 0.5) {
    $ch += 32;
  }
  $pw .= chr($ch);
}

0
 
LVL 2

Author Comment

by:chrisbolt
ID: 1830103
Thank you, worked like a charm!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1830104
srand(time + $$) may be less likely to repeat when both time and $$ increase by one.
(or in 5.004 or later, just omit the srand, as Gunnar sugests)
seeding srand this way may also make it too easy for someone to guess what password was generated. especially if they can get a general idea of when it was generated.
And if you generate a password for one person, they may be able to figure out your clock setting by trying various seeds until they generate the same passord,
and then have a good guess about what passwords you may be generating for others.

you can also say
@letters = ('a'..'z','A'..'Z');
to initialize the array (just once, not for each itteration)
or
 $randpassword = join'',@letters[map{rand @letters}(1)x6];
to generate 6 random letters
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question