Front page | perl.perl5.porters |
Postings from February 2000
bad default srand seed?
Thread Next
From:
Tom Christiansen
Date:
February 9, 2000 04:09
Subject:
bad default srand seed?
Message ID:
3980.950098180@chthon
Witness:
% repeat 20 perl -le '@n = sort { rand() <=> rand() } 1 .. 20; print "@n"'
17 5 4 7 1 14 12 3 11 9 20 13 8 6 16 19 15 18 2 10
17 5 4 7 1 14 12 3 11 9 20 13 8 6 16 19 15 18 2 10
2 18 4 17 3 9 1 12 10 14 8 6 19 13 16 11 7 15 5 20
2 18 4 17 3 9 1 12 10 14 8 6 19 13 16 11 7 15 5 20
17 13 1 6 5 4 9 7 16 20 10 12 18 15 8 11 3 19 14 2
14 9 5 17 8 7 16 10 1 18 13 12 6 15 4 2 19 3 11 20
2 18 5 6 9 3 4 10 1 14 20 11 13 15 19 17 8 16 7 12
17 18 14 7 19 16 15 12 10 11 2 3 6 5 9 4 13 8 1 20
14 1 3 7 13 8 6 19 5 9 10 12 4 20 11 15 16 2 18 17
6 10 7 15 4 17 3 16 13 9 12 19 1 2 5 8 14 11 18 20
11 18 3 8 4 15 2 19 10 12 16 9 6 7 13 17 14 1 5 20
3 7 17 19 4 14 15 11 12 6 10 9 2 8 16 18 1 13 5 20
3 7 17 19 4 14 15 11 12 6 10 9 2 8 16 18 1 13 5 20
18 1 6 9 13 5 12 10 20 17 19 11 15 7 14 16 3 8 4 2
18 1 6 9 13 5 12 10 20 17 19 11 15 7 14 16 3 8 4 2
5 19 6 20 7 1 3 12 11 18 2 9 4 14 10 16 15 17 13 8
11 19 1 2 16 17 6 13 9 8 3 12 5 14 20 4 15 7 10 18
1 20 15 13 4 6 7 8 19 10 5 16 2 9 11 12 18 17 14 3
10 13 1 19 4 20 5 11 18 7 3 8 12 15 14 2 17 6 16 9
11 17 18 3 5 6 9 20 12 2 16 4 13 14 8 10 15 1 7 19
Now, that isn't particularly random. Hm....
% repeat 20 perl -le 'srand; @n = sort { rand() <=> rand() } 1 .. 20; print "@n"'
15 14 18 1 3 6 11 4 13 12 17 10 5 9 20 7 16 8 19 2
17 20 19 5 9 13 8 4 16 3 18 11 10 7 15 1 12 2 14 6
16 1 2 9 7 20 6 12 3 13 8 10 14 15 19 11 18 17 5 4
13 1 3 15 9 6 17 14 4 20 10 18 16 12 7 19 11 2 5 8
13 1 16 18 5 4 15 17 8 9 10 12 14 19 7 3 2 11 6 20
13 1 16 18 5 4 15 17 8 9 10 12 14 19 7 3 2 11 6 20
2 18 4 11 14 5 15 3 16 7 6 1 10 17 12 13 19 8 9 20
2 18 4 11 14 5 15 3 16 7 6 1 10 17 12 13 19 8 9 20
2 1 4 9 19 18 3 14 17 16 10 20 5 13 7 8 15 6 11 12
7 12 3 19 11 13 10 18 4 15 8 17 14 16 5 9 20 2 1 6
7 12 19 11 15 14 9 8 2 4 20 10 16 6 18 1 5 13 3 17
17 7 11 4 2 19 3 10 1 6 18 8 15 20 13 9 5 16 12 14
7 2 13 8 11 16 4 17 1 9 19 12 20 18 15 5 10 6 3 14
2 18 14 11 8 19 12 3 1 16 6 10 5 20 15 4 7 17 13 9
6 18 1 2 3 7 9 12 5 15 19 17 10 11 4 14 20 8 16 13
5 1 3 4 20 2 17 18 11 7 10 14 13 8 6 16 12 9 15 19
5 1 3 4 20 2 17 18 11 7 10 14 13 8 6 16 12 9 15 19
10 2 12 16 7 19 20 5 18 14 11 1 9 3 8 17 6 13 15 4
10 2 12 16 7 19 20 5 18 14 11 1 9 3 8 17 6 13 15 4
12 11 3 17 20 8 6 18 2 13 5 19 10 7 1 9 14 16 15 4
Oh, curses, is there something wrong with the default srand?
That's with 5.005_54 on OpenBSD. Running with v5.5.650 produces
no better results then did 54. I smell a bad seed. Seeding it
myself is *much* better.
% repeat 20 perl -le 'srand(time() ^ ($$ + ($$ << 15))); @n = sort { rand() <=> rand() } 1 .. 20; print "@n"'
19 16 4 20 12 13 10 2 17 9 1 18 3 14 15 6 7 5 8 11
6 18 7 19 20 10 5 9 3 8 14 12 13 15 11 16 17 4 1 2
9 18 15 1 3 5 16 8 19 20 11 4 12 7 10 13 2 17 6 14
2 20 17 19 3 5 7 4 8 9 14 10 18 13 11 15 1 16 6 12
1 12 17 4 15 18 20 13 5 9 7 19 10 6 3 14 16 2 8 11
5 2 11 8 16 19 17 10 9 6 3 4 13 15 12 7 14 1 18 20
19 6 7 18 2 12 14 10 3 9 4 17 16 20 8 15 11 13 1 5
1 9 17 16 18 2 15 7 12 11 4 3 5 10 14 13 6 8 19 20
6 15 3 20 11 2 19 13 12 9 8 10 18 5 16 7 17 14 1 4
3 2 11 14 15 18 13 12 16 4 17 20 8 9 10 1 7 5 6 19
7 2 19 14 9 12 8 20 6 10 4 18 13 5 1 3 15 11 16 17
2 6 1 9 5 7 14 17 18 16 20 10 8 12 15 4 3 11 19 13
1 11 17 4 13 12 16 7 8 18 2 20 10 19 14 15 6 9 3 5
6 12 5 11 8 10 2 1 18 19 16 14 13 20 7 4 3 9 15 17
14 11 17 5 2 20 15 18 1 19 10 3 16 4 7 12 6 8 13 9
13 6 16 10 12 2 17 20 8 7 1 4 18 11 9 15 3 19 5 14
13 9 8 19 20 11 18 5 16 7 3 2 17 6 15 14 1 12 10 4
8 18 12 5 11 17 1 10 3 9 2 16 6 13 14 7 15 20 4 19
2 3 9 17 18 13 8 1 19 14 12 10 4 11 7 20 16 15 6 5
11 7 17 14 4 12 8 18 10 20 19 16 3 15 5 1 13 2 9 6
If I don't seed it, every run of 20 produces at least
one pair of adjacent duplicated lines.
% repeat 100 perl -le 'srand; @n = sort { rand() <=> rand() } 1 .. 20; print "@n"' | uniq | wc -l
81
% repeat 100 perl -le 'srand; @n = sort { rand() <=> rand() } 1 .. 20; print "@n"' | uniq | wc -l
79
% repeat 100 perl -le 'srand; @n = sort { rand() <=> rand() } 1 .. 20; print "@n"' | uniq | wc -l
81
% repeat 100 perl -le 'srand; @n = sort { rand() <=> rand() } 1 .. 20; print "@n"' | uniq | wc -l
79
% repeat 100 perl -le 'srand; @n = sort { rand() <=> rand() } 1 .. 20; print "@n"' | uniq | wc -l
81
% repeat 100 perl -le 'srand; @n = sort { rand() <=> rand() } 1 .. 20; print "@n"' | uniq | wc -l
81
% repeat 100 perl -le 'srand; @n = sort { rand() <=> rand() } 1 .. 20; print "@n"' | uniq | wc -l
82
If I use a proper seed, like srand(time() ^ ($$ + ($$ << 15))),
then I never *once* get any answer but 100 above. Or run with 20
repeats, any answer but 20. And it doesn't matter whether my
sequence of numbers if 10, 20, 100, or 1000 -- there are still
around 20% dups, give or take a few.
And yes, I've tested this on the latest and greatest.
This seems to say that the goal of not having to call srand
has not yet been met.
Ideas?
--tom
Thread Next
-
bad default srand seed?
by Tom Christiansen