Jump to content

Talk:Subtractive generator: Difference between revisions

→‎So... what am I doing wrong?: is the example correct?
(So... what am I doing wrong?)
 
(→‎So... what am I doing wrong?: is the example correct?)
Line 12:
 
Is the algorithm really this bad? Or am I doing something wrong? --[[User:Rdm|Rdm]] 15:14, 2 August 2011 (UTC)
: Did you get the sequence as in example with seed 292929? The following code<lang perl>use 5.10.0;
use strict;
 
my @state;
my $mod = 1_000_000_000;
 
sub init_state {
my @s = ( shift() % $mod, 1);
push @s, ($s[-2] - $s[-1]) % $mod while @s < 55;
 
@state = ();
$state[ (21 * $_) % 55 ] = $s[$_] for (0 .. 54);
 
subrand() for (55 .. 219);
}
 
sub subrand()
{
my $x = ($state[-55] - $state[-24]) % $mod;
#shift @state;
push @state, $x;
return $x;
}
 
init_state(292929);
for (0 .. 9, 210 .. 219) { say "$_: $state[$_]"; }
say "";
for (1 .. 10) { say scalar(@state), ": ", subrand(); }</lang> gave me different answers. --[[User:Ledrug|Ledrug]] 17:32, 2 August 2011 (UTC)
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.