[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Random password generation

Posted on 1998-12-13
3
Medium Priority
?
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Accepted Solution

by:
Gunnar earned 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

650 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