in reply to Rand() with ?log10? distribution?
It won't be exactly a log 10 distribution, but rand()**2 * $max would probably be close to what you're looking for.
For example:
$rand_max = 1025; sub my_rand{ $rnd = rand; return int($rnd*$rnd * $rand_max); } @a = (); for(1..100000) { $n = my_rand(); $a[$n]++; } $i = 1; while($i <= 1024) { print "$i: ", $a[$i], $/; $i = $i*2; }
will produce a distribution similar to this:
1: 1338 2: 913 4: 718 8: 526 16: 397 32: 280 64: 195 128: 137 256: 99 512: 65 1024: 42
If the curve is too steep (or not steep enough), you can adjust the exponent to 1.5, or 2.5 etc., instead of 2.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Rand() with ?log10? distribution?
by BrowserUk (Patriarch) on Jun 07, 2013 at 05:47 UTC |
In Section
Seekers of Perl Wisdom