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 | [reply] [d/l] [select] |