(crazyinsomniac) Re: perl rand() function
by crazyinsomniac (Prior) on Apr 16, 2002 at 09:59 UTC
|
The pod for the rand function says
rand Returns a random fractional number greater than or equal to "0"
So yes, it is zero inclusive.
The return data type is not float (there is no such thing in perl). See perldata and perlnumber for more information (and a better explanation of perl number semantics -- to you/me they're all scalars, but some functions/operators are picky).
Back to perldoc, perlfunc says:
Numeric functions
"abs", "atan2", "cos", "exp", "hex", "int", "log", "oct", "rand",
"sin", "sqrt", "srand"
see int -- it just returns the integer portion of a number. It does not round. If you wish to round, the int documentation itself reccomends "sprintf", "printf", or the "POSIX::floor" and "POSIX::ceil". There is a tutorial on (s)printf in our Tutorials section. POSIX is a standard module, and ceil and floor do what you'd expect.
Now on a different note, it's easy to miss int tucked in the perlfunc, so do like How to RTFM says (great read), use the help of a search engine or even grep/find, especially when you've got a great keyword like round.
update: moodster below links to a question from the perlfaq which is invaluable (and which is also mentioned in How to RTFM -- good advice).
| [reply] |
|
|
Thanks for the info and all the links. It completly jumped out of my mind to use sprtintf/printf but I hadn't of heard of the POSIX module.
Thanks for the heads up!
--
Wiliam Stephens <wil@stephens.org>
| [reply] |
Re: perl rand() function
by moodster (Hermit) on Apr 16, 2002 at 10:04 UTC
|
| [reply] [d/l] [select] |
(MeowChow) Re: perl rand() function
by MeowChow (Vicar) on Apr 16, 2002 at 10:46 UTC
|
Yes, it is zero inclusive, however, I wouldn't be waiting around for perl -e '1 while rand' to finish within my lifetime.
MeowChow
s aamecha.s a..a\u$&owag.print | [reply] [d/l] |
|
|
% perl -de 0
...
DB<1> $c=0; print ''.localtime(),$/; $c++ while rand; print ''.local
+time(),$",$c,$/
Tue Apr 16 07:57:24 2002
Tue Apr 16 07:57:24 2002 55285
DB<2> $c=0; print ''.localtime(),$/; $c++ while rand; print ''.local
+time(),$",$c,$/
Tue Apr 16 07:57:28 2002
Tue Apr 16 07:57:28 2002 82230
;)
Update: To be more informative rather than just humorous, I'll note that
perl -MConfig -e 'print $Config{randbits}'
reports 15 for me. If I managed to build Perl such that this value got all the way up to 32, then I'd project MeowChow's life expectancy at a bit under 45 minutes. If I got that up to 48, then we'd be talking closer to 5 years. And if I got it up to 64, then I'd agree with MeowChow that waiting around 350,000 years wasn't worthwhile. (:
- tye (no, I don't own a Cray) | [reply] [d/l] [select] |
|
|
tye, you absolutely slay me :)
MeowChow
s aamecha.s a..a\u$&owag.print | [reply] |
Re: perl rand() function
by func (Acolyte) on Apr 16, 2002 at 17:53 UTC
|
I was playing around with rand() last night and wanted rand() to select a random number from a range. After much brain racking and some help from learning perl, I came up with this:
#!/usr/bin/perl -w
use strict;
srand;
# produce n random numbers
my $lowest_number=1;
my $highest_number=49;
my @number_range=($lowest_number...$highest_number);
my @lottery_numbers=(1...6);
my $a = 1;
do {
# A random element from @number_range is selected and
# placed in to @X
#
$lottery_numbers[$a] = $number_range[rand $highest_number];
$a ++;
} until $a > 6;
print "@lottery_numbers[1...6]\n";
While this may not be of use to the original poster, I hope it will be useful to some monk in the future. :)
Yet another perl newbie. | [reply] [d/l] |
|
|
#!/usr/bin/perl
use strict;
my ($beg, $end) = qw(10 20);
my $range = ($end - $beg) + 1;
for (1..6) {
print int(rand($range) + $beg) . "\n";
}
| [reply] [d/l] |
|
|
| [reply] |
|
|
my @number_range=($lowest_number...$highest_number);
a side note: what you've got there is terribly inefficient.
You are constructing an array of size ($highest_number -
$lowest_number) which is going to be pretty painful if your
range is 0 to 100,000,000.
| [reply] |