Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!isi.edu!venera.isi.edu!jas From: jas@ISI.EDU (Jeff Sullivan) Newsgroups: comp.lang.perl Subject: random numbers? Message-ID: <16698@venera.isi.edu> Date: 8 Feb 91 22:11:33 GMT Sender: news@isi.edu Distribution: comp Organization: USC-ISI Lines: 92 Is there some problem with perl's srand and rand functions? I wrote this program which is supposed to generate a random number between 1 and 20, ecept if it produces a 10 on the first (and subsequent) try, in which case it adds the first (and subsequent) 10s to another random number between 1 and 20. This is the code: #!/usr/bin/perl srand; $total = $roll = int(1+rand(20)); while ($roll == 10) { $roll = int(1+rand(20)); $total += $roll; } print $total,"\n"; I wrote a test program to run this a lot of times in a row to test it. Here's the program: #!/usr/bin/perl foreach (0..$ARGV[0]) { print `roll`; } Here's the output: 17 7 7 7 18 18 8 18 19 19 9 9 19 19 19 *24 *24 *24 20 20 20 20 20 20 *25 *25 *25 *25 *25 *25 20 20 20 20 11 11 11 11 11 1 1 1 1 1 11 11 Hardly looks random to me. What am I doing wrong? jas -- -------------------------------------------------------------------------- Jeffrey A. Sullivan | Senior Systems Programmer jas@venera.isi.edu | Information Sciences Institute jas@isi.edu | University of Southern California