You are looping 250 times, generating numbers between 0 and 100. Try this:
#! /usr/bin/perl -w
use strict;
print int (rand (251) + 250), "\n" for (1..100);
Update: Thanks to blue_cowdawg and pizza_milkshake, changed the 250 to 251. | [reply] [d/l] |
this will generate numbers between 250 and 499 because of the behaviors of rand and int:
rand Returns a random fractional number greater than or equal to 0 and less than the value of EXPR.
int Returns the integer portion of EXPR. If EXPR is omitted, uses $_. You should not use this function for rounding: one because it trun-cates towards 0, and two because machine representations of floating point numbers can sometimes produce counterintuitive results.
rand(250) will always return a number less than 250 and int will never round up.
use rand(251)
perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"
| [reply] |
What do we mean by "between"? Your code works fine, but it includes 250 and excludes 500.
| [reply] |
Apply "int()" to the value returned by "rand()" if you
+want
random integers instead of random fractional numbers.
+For
example,
int(rand(10))
returns a random integer between 0 and 9, inclusive.
With this in mind take a look at my reply and apply a
small tweak:
my @answers=();
push @answers,(int(rand(251))+250) foreach (0..99);
The sequence int(rand(251)) will now produce
integers in the range of 0..250 and when added to 250
will produce 250..500.
| [reply] [d/l] [select] |
| [reply] |
TMTOWTDI, and here's a way to ensure that the same number isn't selected more than once:
use strict;
use warnings;
use List::Util qw(shuffle);
my @numbers = (shuffle(250 .. 500))[0 .. 99];
print "@numbers\n"
Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
| [reply] [d/l] |
Greetings Fellow Monk!
Here is yet another way to do what you are asking for using the CPAN Module Math::Random.
#!/usr/bin/perl -w
use strict;
use Math::Random;
my($n,$low,$high)=(100,250,500);
my @array=random_uniform_integer($n,$low,$high);
for(my $i=0; $i<$n; $i++){print "$array[$i]\n";}
$n is the number of random numbers you wish to generate,
$low is the lower limit, $high is the upper limit, and
@array is the array containing your random numbers as specified by your lower and upper limits.
The beauty of Perl, The Perl Community, and CPAN is that there is usually a module out there that will do just about anything you are looking for often times significantly reducing the amount of code you need to write. | [reply] [d/l] |
my @answers=();
push @answers,(int(rand(250)+250) foreach (0..99);
Completely untested...
| [reply] [d/l] |