Re: random number with range
by GrandFather (Saint) 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
| [reply] [d/l] [select] |
|
ok, that works , but why you add +1 in the end? i see that without it, the generator does not generate full lenght( te top number never apiers) but with +1 its all right, where does the 1 goes that we need manually add it?
| [reply] |
|
perl -E 'say int 0.999999999'
0
| [reply] [d/l] [select] |
Re: random number with range
by Polonius (Friar) on Jun 01, 2006 at 17:37 UTC
|
| [reply] [d/l] |
Re: random number with range
by salva (Canon) 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. | [reply] [d/l] [select] |
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)];
| [reply] [d/l] |
|
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
| [reply] [d/l] |
|
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)];
| [reply] [d/l] [select] |
|
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 ) ]);
| [reply] [d/l] |
Re: random number with range
by borisz (Canon) on Jun 01, 2006 at 17:35 UTC
|
use List::Util 'shuffle';
my @nums = shuffle( 50 .. 200 );
| [reply] [d/l] |
|
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 dependent 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 dependent on the previously selected numbers, no lists are built and maintained, and thus, time and memory requirements are only O(1).
| [reply] |