Apart from not being what the OP wants and not being Perl so much a transliterated C, that code actually demonstrates the problem the OP was most likely to have had with the default Perl rand. Consider:
use strict; use warnings; my %randLines; ++$randLines{1 + int rand 40_000_000} for 1 .. 20_000_000; my @hits = map {[$_, $randLines{$_}]} sort {$randLines{$b} <=> $randLines{$a}} keys %randLines; print scalar @hits, " different lines selected\n"; print "Line $_->[0] hit $_->[1] times\n" for @hits[0 .. 9];
Prints:
32768 different lines selected Line 3532715 hit 724 times Line 24512940 hit 718 times Line 20959473 hit 712 times Line 28502198 hit 705 times Line 4688721 hit 704 times Line 37175293 hit 700 times Line 26921387 hit 699 times Line 28406983 hit 699 times Line 3172608 hit 696 times Line 31093751 hit 695 times
Note in particular that only 32768 (very close to 215 btw) different lines were selected out of the 20,000,000 the OP was after!
The actual results will vary with the specific build of Perl with the result shown being about the worst you are likely to encounter and are because the rand for the build of Perl used to run the sample uses only a 15 bit value. Much better results are obtained if you add the line use Math::Random::MT qw(rand srand); to the sample, but be prepared to wait a while and make sure your system has lots of memory available.
In reply to Re^2: A series of random number and others
by GrandFather
in thread A series of random number and others
by lightoverhead
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |