Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

RE: Random number

by Anonymous Monk
on Jul 10, 2000 at 01:20 UTC ( #21725=note: print w/replies, xml ) Need Help??

in reply to Random number

my @Chars = ( "A" .. "Z", "a" .. "z", 0 .. 9); my $RandString = join("", @Chars[ map { rand @Chars } ( 1 .. 30 ) ]);
Here's how I generate random strings. You can just have 0 .. 9 in @Chars if you want an integert. And change 1 .. 30 to loop however many times you like - this example creates a random string that is 30 chars long.

Replies are listed 'Best First'.
RE: RE: Random number
by greenhorn (Sexton) on Jul 10, 2000 at 11:53 UTC


    my $RandString = join("", @Chars[ map { rand @Chars } ( 1 .. 30 ) ]);

    I'm wondering if I may inflict a question upon you--details about
    that statement. I understand the purposes of "components" within it,
    and I can certainly see the results when I run the script. But just
    how it's generating those results is not clear to me. Could I talk you
    into explaining a bit about how it works?


      It's not too tricky. The rand statement imposes a scalar context on @Chars, so it gets a random number bounded by the number of elements in the array.

      The map statement creates a 30 element list of those random numbers.

      That list is used as indexes into an array slice. (That means that the second argument to the join statement is a random set of 30 characters from the @Chars array.) They're joined into a string.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://21725]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2022-10-06 07:53 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (26 votes). Check out past polls.