Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: rand() function on Windows systems (pure Perl)

by BrowserUk (Patriarch)
on Dec 30, 2016 at 21:57 UTC ( [id://1178696]=note: print w/replies, xml ) Need Help??


in reply to Re^2: rand() function on Windows systems
in thread rand() function on Windows systems

Just for completeness, this pure perl version should also work:

{ my $seed; sub ppSrand{ $seed = int( $_[ 0 ] & 32767 ); } sub ppRand{ my $max = shift // 1; $seed = ( $seed * 214013 + 2531011 ); return ( ( $seed >> 16 ) & 32767 ) / 32768 * $max; ## Amend +ed per post below. } } ppSrand( 555 ); print int ppRand( 1000 );

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: rand() function on Windows systems (pure Perl)
by BillKSmith (Monsignor) on Dec 31, 2016 at 05:03 UTC
    Thanks, BrowserUK. I have a low priority project which has been on hold for several years awaiting a solution to the problem described by the OP. Somehow, I overlooked the possibility of writing a generator. (The quality of the sequence is not an issue.) Your solution seems perfect!
    Bill
Re^4: rand() function on Windows systems (pure Perl)
by bakiperl (Beadle) on Dec 30, 2016 at 23:23 UTC
    Wow! This is even better. Thank you.

      Hm. I forgot that Perl would upgrade the seed to a float; that needs to be clamped back to a 32-bit integer as the new line below:

      { my $seed; sub ppSrand{ $seed = int( $_[ 0 ] & 32767 ); } sub ppRand{ my $max = shift // 1; $seed = ( $seed * 214013 + 2531011 ); $seed &= 0xffffffff; return ( ( $seed >> 16 ) & 32767 ) / 32768 * $max; } }

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Great job man! This last fix cleared all my problems.

        Thank you again.

Re^4: rand() function on Windows systems (pure Perl)
by stevieb (Canon) on Dec 31, 2016 at 00:08 UTC

    That's very nice. I'm glad we have statistical analysts/mathematicians here.

      Small correction in the code above:

      This line of code :

      return ( ( $seed >> 16 ) & 32767 ) / 32767 * $max;

      should be:

      return ( ( $seed >> 16 ) & 32767 ) / 32768 * $max;
      The difference can be seen if you don't use the integer (int) function.
        The difference can be seen if you don't use the integer (int) function.

        You are absolutely correct++!


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice.
      I'm glad we have statistical analysts/mathematicians here.

      He he. I am abysmal at statistics -- it is the second most unintuitive subject on the planet -- and I am to a mathematician, what a draughtsman is to an artist.

      I did a search and found this, the conversion to Perl was trivial :)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-29 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found