Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: random number with range

by Enlil (Parson)
on Jun 01, 2006 at 17:43 UTC ( #553101=note: print w/replies, xml ) Need Help??

in reply to random number with range

my @range = ( 50 .. 200 ); my $random_number = $range[rand(@range)];

Replies are listed 'Best First'.
Re^2: random number with range
by GrandFather (Sage) on Jun 01, 2006 at 18:00 UTC

    An interesting way to do it. But expensive in memory and time for large ranges. Consider:

    my @range = ( -1_000_000 .. 1_000_000 ); my $random_number = $range[rand(@range)];

    which takes about 1/2 a second and has about a 100 MB dynamic memory requirement.

    DWIM is Perl's answer to Gödel

      Indeed, but the technique is particularly useful for sets. For example,

      my @set = ( 1, 2, 3, 5, 8, 13, 21 ); my $random = $set[rand(@set)];
      my @set = qw( apple orange banana cucumber ); my $random = $set[rand(@set)];
      won't disagree, but first thing that came to mind for similar situations where the technique is useful, like the ubiquitous password generator:
      my @chars = ( 'A' .. 'Z', 0 .. 9); my $pw = join('',@chars[ map { rand @chars } ( 1 .. 12 ) ]);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://553101]
erix is always in favour of code-revolutions
[Lady_Aleena]: Well, right now, I"m looking at modules where I put randomness that aren't in my RolePlaying/Random dir, like RolePlaying:: Character:: Alignment that is mostly about random_alignment than expand_alignment.
[erix]: cute

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2017-05-27 20:37 GMT
Find Nodes?
    Voting Booth?