but I would prefer a solution that works on Linux as well
On Linux, I'm finding that the behaviour hasn't changed. That is, when I run:
perl -e 'srand(0); print int(rand(99)) for 0 .. 9'
on Ubuntu, I get
1674986577768368673 from (current) perl-5.30.0 all the way back to perl-5.6.2.
Or do you mean that you want the output of the one-liner to also be
023647263525973192 on Linux ?
That could be tricky ... I'm not sure what that might involve.
Here's an Inline::C version that does as you want on Windows - but produces a different sequence of numbers (
83397779901933762754) on Ubuntu, where RAND_MAX is 2147483647.
use strict;
use warnings;
use Inline C => <<'EOC';
SV * max_rand() {
return newSViv(RAND_MAX);
}
void my_srand(int seed) {
srand(seed);
}
SV * my_int_rand(IV limit) {
return newSViv(rand() * limit / RAND_MAX);
}
SV * my_float_rand(IV limit) {
return newSVnv((NV)rand() * limit / RAND_MAX);
}
EOC
print "RAND_MAX is: ",max_rand(), "\n";
my_srand(0);
print my_int_rand(99) for 0 .. 9;
#print int(my_float_rand(99)) for 0 .. 9;
Annoyingly on Ubuntu, although perl's rand() output seems constant across different versions of perl, it differs from the output of that script.
Another possibility is that Math::Random might provide the functionality you seek. (I took a quick look, couldn't find anything helpful, and gave up ... but I didn't check it out rigorously.)
Cheers,
Rob
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.