root/Swishetest/trunk/NotRand.pm

Revision 1965, 1.1 kB (checked in by joshr, 8 months ago)

tabs replaced with spaces. We use ts=4 sw=4 in vi.

Line 
1 package NotRand;
2 require Exporter;
3 @ISA = qw(Exporter);
4 @EXPORT_OK = qw(not_rand);  # symbols to export on request
5
6
7 # *repeatable* semi-random-ish integer number generator
8 # we deal with overflow by truncating to 30 bits , so this never returns an int
9 # larger than about 2**30
10 # this implementation from "Advanced Perl Programming", 4.4 Using Closures,
11 # but we use global $last instead of a closure.
12 # I can't imagine that this ever returns numbers that look very random,
13 # as such things go...
14 use vars qw( $last );
15 sub not_rand {
16     my $max = $_[0] || 1;   # if no value is passed, we return '0' or '1'
17     use integer;    # is it faster not to use integer? No, it's faster to USE int.
18     $last = 1 unless defined($last);
19     $last = ($last*21+1);   
20         # from "Advanced Perl Programming", 4.4 Using Closures.
21         # We truncate to 30 bits to preclude system overflow and thereby be more portable
22         # that would be 'mod 2 ** 30' (1,073,741,824), which makes sense, 4.2G over 4
23     $last %= 1_073_741_824;  # that's 2 to the 30th
24     #print "rand of $max is " . abs($last % $max) . "\n";
25     return abs($last % $max);   # abs isn't needed
26 }
27
28 1;
Note: See TracBrowser for help on using the browser.