Re^3: rand() function on Windows systems (pure Perl)
by BrowserUk (Patriarch) on Dec 30, 2016 at 21:57 UTC
|
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.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] [d/l] |
|
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!
| [reply] [Watch: Dir/Any] |
|
Wow! This is even better. Thank you.
| [reply] [Watch: Dir/Any] |
|
{
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.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] [d/l] |
|
|
| [reply] [Watch: Dir/Any] |
|
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
|
|
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.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] |