Problems? Is your data what you think it is? PerlMonks

### random number with range

 on Jun 01, 2006 at 17:25 UTC Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone help me figure something out? I want to randomly choose a number between a given range (say between the numbers 50 and 200).

How can I do this? I know I could create a dynamic array with these values and use Algorithim::Numerical::Shuffle and just pick one, but was wondering if there was a better way. The level of randomness isn't very important.

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

Just use the rand function. It generates numbers in a half open interval ranging from (and including) 0 through to (but not including) the positive number given (or 1 if omited). The result is a real number (not an integer) in that range. If you want to pick an integer between \$start and \$end inclusive you can do this:

```my \$number = \$start + int rand \$end - \$start + 1;

\$end must be >= \$start.

DWIM is Perl's answer to Gödel
Re: random number with range
by Polonius (Friar) on Jun 01, 2006 at 17:37 UTC

Assuming you want an integer from 50 to 200 inclusive:

int (rand 151) + 50
Polonius
Re: random number with range
by salva (Abbot) on Jun 01, 2006 at 17:34 UTC
```my \$num = \$min + rand(\$max - \$min);
or...
```my \$int = \$min + int(rand(\$max - \$min));
if you want integers.
Re: random number with range
by Enlil (Parson) on Jun 01, 2006 at 17:43 UTC
```my @range = ( 50 .. 200 );
my \$random_number = \$range[rand(@range)];

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 ) ]);
Re: random number with range
by borisz (Canon) on Jun 01, 2006 at 17:35 UTC
Here is one way:
```use List::Util 'shuffle';
my @nums = shuffle( 50 .. 200 );
Boris

Yes, that works. And that method is great if you want to grab multiple random numbers such that you never get the same one twice. For example, implementing a shuffled deck of cards, or drawing names out of a hat. But if all the OP wants is a single random number, or lots of random numbers uninhibited by such rules as "the number can never repeat", that solution is probably not the best way to do it.

With a shuffled list you have the following problems:

• Each number can only exist once, or at least a predetermined finite number of times. True random numbers are not dependant on what has already been picked. If you want only four aces to exist in a deck, your solution is great. If you want each subsequent draw to be from a 'fresh' deck, your method would require a new deck, and a new shuffle after each draw.
• The actions of building the deck (the list), shuffling it, and copying it to @num each take O(n) time.
• For any range of randoms, your 'deck' grows in memory requirements in a 1:1 relationship ( O(n) ). Thus, a huge range will require huge amounts of memory.

On the other hand, simply picking a random number using rand insures that each subsequent random number isn't dependant on the previously selected numbers, no lists are built and maintained, and thus, time and memory requirements are only O(1).

Dave

Create A New User
Node Status?
node history
Node Type: perlquestion [id://553094]
Approved by GrandFather
help
Chatterbox?
 [thezip]: G'day! [Corion]: Hi thezip! [thezip]: How goes it, Corion?

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2017-02-23 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Before electricity was invented, what was the Electric Eel called?

Results (349 votes). Check out past polls.