hangon has asked for the wisdom of the Perl Monks concerning the following question:
I need to randomize large integers (> 32K). I looked at some modules, but I just need to call $foo = rand $bar repeatedly without setting up objects & parameters. So, I came up with the following scheme to extend the range of rand to around 1G. Basically, it breaks $bar into blocks of nearly equal size (within +1 -0) and calls rand twice, once to select a block, and again to select the number within the block, then it totals everything up. It appears to DWIM and seems a reasonable approximation of rand (the distribution of numbers generated compares closely to rand), but I don't have the expertize to test it rigorously.
It's to be used for simulation, and doesn't need to be perfect. Does this seem like a reasonable approach? Am I overlooking any potential problems?
sub bigrand{ my $in = shift; my $limit = 32768; if ($in <= $limit){ return int rand $in; } my $blockSize = int($in ** .5); my $blockSelect = int rand int ($in/$blockSize); my $leftOver = $in % $blockSize; if ($blockSelect < $leftOver){ return ($blockSelect * ($blockSize + 1)) + int rand ($blockSize + 1); }else{ return ($leftOver * ($blockSize + 1)) + (($blockSelect - $leftOver) * $blockSize) + int rand ($blockSize); } }
Update: It's been an education. Up to now, rand is one of those functions I've just used and taken for granted. Thanks especially to BrowserUK for the explanation and links to other nodes, and to tachyon-II whose module Math::Random::MT::Perl I ended up using.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Randomizing Large Integers
by BrowserUk (Patriarch) on May 19, 2008 at 18:00 UTC | |
by hangon (Deacon) on May 19, 2008 at 20:04 UTC | |
by BrowserUk (Patriarch) on May 19, 2008 at 20:36 UTC | |
by tachyon-II (Chaplain) on May 20, 2008 at 11:10 UTC | |
|
Re: Randomizing Large Integers
by mscharrer (Hermit) on May 19, 2008 at 18:25 UTC | |
by almut (Canon) on May 19, 2008 at 19:48 UTC | |
by mscharrer (Hermit) on May 19, 2008 at 20:29 UTC | |
by BrowserUk (Patriarch) on May 19, 2008 at 21:25 UTC | |
by ikegami (Patriarch) on May 20, 2008 at 16:59 UTC | |
by hangon (Deacon) on May 19, 2008 at 20:23 UTC | |
by syphilis (Archbishop) on May 20, 2008 at 08:01 UTC | |
by ysth (Canon) on May 21, 2008 at 04:49 UTC |