Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Re: Random string generator

by ibanix (Hermit)
on Feb 06, 2003 at 02:06 UTC ( #233033=note: print w/replies, xml ) Need Help??

in reply to Re: Random string generator
in thread Random string generator

This is really nifty. Paladin++

I have to spell this out; perhaps other monks will appreciate the explination. Please feel free to correct me.

rand @chars takes @chars in scalar context, that is, the length of the array holding our characterset. So rand returns a value somewhere in the array, and that becomes the index for the array lookup.

I keep forgetting you can do cool things like this by implied scalar or array context.


$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: Re: Re: Random string generator
by bart (Canon) on Feb 06, 2003 at 11:29 UTC
    Don't forget to add that using a floating point number as an array index, does an implied int on the value. In other words: it truncates towards zero. $a[5.9] is the same array element as $a[5], not $a[6].

    A very explicit version would have been:

    $chars[int rand scalar @chars]
    which does exactly the same thing as
    $chars[rand @chars]
    The net effect is that all array indexes have the same change of being chosen.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2022-08-17 15:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found