Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Old random number generator

by Pascal666 (Scribe)
on May 10, 2020 at 04:45 UTC ( #11116638=note: print w/replies, xml ) Need Help??


in reply to Re^3: Old random number generator
in thread Old random number generator

Thank you. I don't mind calling my_srand and my_rand (no need to override srand and rand), but I would prefer a solution that works on Linux as well. The * $_[0] / 32767 part appears to be what I needed. print int(winrand()*99/32767) for 0..9 appears to work beautifully. Thank you everyone for your assistance!

Replies are listed 'Best First'.
Re^5: Old random number generator
by syphilis (Bishop) on May 10, 2020 at 06:47 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11116638]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2020-10-24 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (242 votes). Check out past polls.

    Notices?