rand and srand 
Author Message
 rand and srand

I noticed that some supposedly random selections seemed to have a
lot in common from day to day, so tried this:

% perl -we 'for ($i=0;$i < 100;$i++) { srand $i; printf "%d %d\n",
$i, rand(4)}
;'
0 0
1 2
2 0
3 2
4 0
5 2
6 0
7 2
8 0
9 2
10 0
11 2
12 0
13 2
14 0
15 2
16 0
17 2
18 0
19 3
20 1
21 3
22 1
23 3
24 1
25 3
26 1
27 3
28 1
29 3
30 1
31 3
32 1
33 3
34 1
35 3
36 1
37 0
38 2
39 0
40 2
41 0
42 2
43 0
44 2
45 0
46 2
47 0
48 2
49 0
50 2
51 0
52 2
53 0
54 2
55 1
56 3
57 1
58 3
59 1
60 3
61 1
62 3
63 1
64 3
65 1
66 3
67 1
68 3
69 1
70 3
71 1
72 3
73 2
74 0
75 2
76 0
77 2
78 0
79 2
80 0
81 2
82 0
83 2
84 0
85 2
86 0
87 2
88 0
89 2
90 0
91 3
92 1
93 3
94 1
95 3
96 1
97 3
98 1
99 3

Note the dual alternation of first-generated numbers, depending on
the seed.

The man page is pretty sketchy on how srand and rand work.  Maybe
I'm just not getting it.  What's the right way to select m
(typically about 5) items randomly from a list of n (typically
about 40) items, run from cron at a certain time each day, so that
I'll get all the n items included in the short list with about
equal frequency over many trials? materially different output from
day to day?

Here's the program I was using, whose run-to-run commonality makes
it seem flawed:

#!/usr/bin/perl
# randomly reorder the lines on stdin

srand($$);



$i = 0;
while ($i < $want) {
        $j = int rand $lenIn - .0001;
        if ($in[$j]) {
                $i++;

                $in[$j] = '';
        }

Quote:
}



print "$out\n";

Thanks!
 - Irving
--

 Happy Man Corp.  4410 SW Pt. Robinson Rd., Vashon, WA  98070-7399
 We publish SOLID VALUE for the intelligent investor.  NextMail OK



Fri, 19 Jan 1996 04:05:25 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. use of rand and srand

2. Broken rand() and srand() on perl5.005 ??

3. rand() and srand() busted on SVR4?

4. srand or rand question

5. srand and rand fx

6. RAND/SRAND query

7. srand and rand on NTPerl 5.001-1L-091

8. Rand (or not to Rand)

9. Is rand() really rand ?

10. How do I ? .. rand/srand champs read this

11. How many significant digits in srand?

12. Problem with srand(time^$$)

 

 
Powered by phpBB® Forum Software