Gossamer Forum
Home : General : Perl Programming :

Randomness

Quote Reply
Randomness
I need a string that is random. It must have 2 characters followed by 6 digits. I'm currently using the following code:

Code:
$randlet = chr(int(rand(20)) + 99) . chr(int(rand(20)) + 99);
$randlet =~ tr/a-z/A-Z/;
$randnum = 100000 + int(rand(999999));
$randid = $randlet . $randnum;

Can anyone spot any faults with the above code? Any way to better that code? It works fine at the moment. Just unsure of the method used to ensure the length of random numbers to be 6 digits long.

Rgds

Wil

- wil
Quote Reply
Re: [Wil] Randomness In reply to
Above it add:

srand ( time || $$ );

...to create a random seed for rand()
Quote Reply
Re: [RedRum] Randomness In reply to
That's not really needed in Perl 5? Or am I mistaken. I don't really need it that random anyway.

Cheers

Wil

- wil
Quote Reply
Re: [Wil] Randomness In reply to
Well if you don't need it that random then what you have will do I guess.

You can cut out tr and use:

$randlet = uc(chr(int(rand(20)) + 99)) . uc(chr(int(rand(20)) + 99));
$randnum = 100000 + int(rand(999999));
$randid = $randlet . $randnum;

Last edited by:

RedRum: Nov 14, 2001, 8:40 AM
Quote Reply
Re: [RedRum] Randomness In reply to
No, that bit is important. :-)

A better way of writing that would be to use:

Code:
$randlet = chr(65 + int(rand(26))

Because the code I supplied (which you duplicated) will discriminate against a,b,x,y,z, because there are 26 letters in the alphabet and not 20.

This would also make sure they were all UC and thus cutting out my case translation altogether.

- wil
Post deleted by RedRum In reply to

Last edited by:

RedRum: Nov 14, 2001, 8:49 AM
Quote Reply
Re: [Wil] Randomness In reply to
Forgot the closing parentheses - oops.

- wil
Quote Reply
Re: [Wil] Randomness In reply to
>>This would also make sure they were all UC <<

Um yeah my code makes the letters uppercase too TongueTongue

(and works perfectly well...although ugly)

Last edited by:

RedRum: Nov 14, 2001, 8:55 AM
Quote Reply
Re: [RedRum] Randomness In reply to
Yes. I overlooked that, sorry.

- wil
Quote Reply
Re: [RedRum] Randomness In reply to
In Reply To:
Above it add:

srand ( time || $$ );

...to create a random seed for rand()

perldoc -q random:
Quote:
Found in /usr/lib/perl5/5.7.2/pod/perlfaq4.pod
Why aren't my random numbers random?


If you're using a version of Perl before 5.004, you must
call "srand" once at the start of your program to seed the
random number generator. 5.004 and later automatically
call "srand" at the beginning. Don't call "srand" more
than once--you make your numbers less random, rather than
more.

Then, from perldoc -f srand:
Quote:
Note that you need something much more random than
the default seed for cryptographic purposes.
Checksumming the compressed output of one or more
rapidly changing operating system status programs
is the usual method. For example:

srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);

If you're particularly concerned with this, see
the "Math::TrulyRandom" module in CPAN.

So, in short, unless you're programming for 5.003 (5.004 was release 4.5 years ago) don't call srand unless you are calling it with something significantly more random than time() or the process id ($$).

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [jagerman] Randomness In reply to
I'd like to be able to master perl in 24 hours but I'm learning...
Quote Reply
Re: [RedRum] Randomness In reply to
LOL... How long do you think it took Larry Wall to master Perl?

--Philip
Links 2.0 moderator
Quote Reply
Re: [PerlFreak] Randomness In reply to
Ugh everytime someone says Larry Wall I think of larry King.

Has anyone ever thought that he looks like one of those little trolls with illuminous hair? ....do you guys have those?

Last edited by:

RedRum: Nov 14, 2001, 3:04 PM
Quote Reply
Re: [jagerman] Randomness In reply to
Jason

Yeah, I remember reading somewhere that calling srand(); went out in Perl 5.5x.

Rgds
Wil Stephens

- wil
Quote Reply
Re: [Wil] Randomness In reply to
5.004 not 5.5x