?? gb_flip.pl.in,v
字號:
head 1.1;access;symbols zero-five-zero:1.1 zero-four-seventeen:1.1 zero-four-ten:1.1 zero-four-nine:1.1 zero-four-eight:1.1 zero-four-five:1.1 zero-four-three:1.1 zero-four-zero:1.1;locks neto:1.1;comment @# @;1.1date 97.11.01.22.17.43; author neto; state Exp;branches;next ;desc@A Perl version of the Stanford GraphBase random number generator.@1.1log@Initial revision@text@#! @@PERL@@ -w# @@configure_input@@# This is a Perl version of the Stanford GraphBase random number generator.# This file is *not* part of the Stanford GraphBase.# This file is in the public domain, and comes with no warranty.# David Neto, November 1, 1997.use strict;my (@@A);my ($ptr);sub gb_flip_initialize { @@A=(-1);}sub gb_mod_diff { my ($x,$y) = @@_; return ($x-$y) & 0x7fffffff;}sub gb_flip_cycle { my ($j, $i); for $j (32..55) { $A[$j-31] = &gb_mod_diff($A[$j-31],$A[$j] ); } for $i (25..55) { $A[$i] = &gb_mod_diff($A[$i], $A[$i-24]); } $ptr = 54; return $A[55];}sub gb_next_rand { return $A[$ptr] >= 0 ? $A[$ptr--] : &gb_flip_cycle;}sub gb_init_rand { my ($seed) = shift; my ($i, $prev, $next); &gb_flip_initialize; $next = 1; $seed = $prev = &gb_mod_diff($seed,0); $A[55] = $prev; for ( $i=21; $i ; $i = ($i+21) % 55) { $A[$i] = $next; # Section 9: Compute a new next value, based on next, prev, and seed. $next = &gb_mod_diff($prev, $next); if ( $seed & 1 ) {$seed = 0x40000000 + ($seed >> 1);} else {$seed >>= 1;} $next = &gb_mod_diff($next,$seed); # :9 $prev = $A[$i]; } # Section 8: Get the array values warmed up. &gb_flip_cycle; &gb_flip_cycle; &gb_flip_cycle; &gb_flip_cycle; &gb_flip_cycle; # :8}sub gb_unif_rand { my ($m, $r, $t); # Make sure the argument is small enough, and positive. $m = int( shift ) & 0x7fffffff; $t = 0x80000000 - (0x80000000 % $m); do { $r = &gb_next_rand; } while ( $t <= $r ); return $r % $m;}sub gb_test { my ($j); &gb_init_rand(-314159); &gb_next_rand == 119318998 || die("Failure on the first try!"); for $j (1..133) { &gb_next_rand; } &gb_unif_rand(0x55555555) == 748103812 || die("Failure on the second try!"); print STDERR "Ok the gb_flip routines seem to work!\n"; return 0;}# &gb_test;@
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -