Is rand() really rand ? 
Author Message
 Is rand() really rand ?

As part of my never ending quest for financial independence I have
written a short script to pick my lottery numbers - six integers, chosen
from the numbers 1-49, no duplicates.  I have yet to win !

However, I've noticed that each time I call the script it returns the
same numbers.....though these are dependent upon whether I'm using
MacPerl or the NT port.

Here's the relevant part of the script......what am I doing wrong ?

Thanks for the help - I'll tell you if I win  ;-)

David

foreach $r (1..11) {


    $i = 0;
while ($i < 6) {




$i++;

Quote:
}


print "$numbers\n";

Quote:
}

--

______________
David J. Evans
AMS, The University of Reading, Whiteknights, P.O. Box 228, READING, UK
RG6 6AJ
Voice : 44 (0)1734 318893  Fax : 44 (0)1734 316537  
http://www.*-*-*.com/



Sun, 03 May 1998 03:00:00 GMT  
 Is rand() really rand ?
before you call the function rand, call the srand function call to generate the
random seed first, that will probably be giving you a better
random scheme.
--KK Wong


Sun, 03 May 1998 03:00:00 GMT  
 Is rand() really rand ?

Quote:
> However, I've noticed that each time I call the script it returns the
> same numbers.....though these are dependent upon whether I'm using
> MacPerl or the NT port.

You might find something interesting if you search for 'srand' in the
perlfunc man page. Hope this helps!

-- Tom Phoenix            http://www.teleport.com/~rootbeer/            




Sun, 03 May 1998 03:00:00 GMT  
 Is rand() really rand ?

Quote:
g.ac.uk> writes:

:>
:>As part of my never ending quest for financial independence I have
:>written a short script to pick my lottery numbers - six integers, chosen
:>from the numbers 1-49, no duplicates.  I have yet to win !
:>
:>However, I've noticed that each time I call the script it returns the
:>same numbers.....though these are dependent upon whether I'm using
:>MacPerl or the NT port.
:>
:>Here's the relevant part of the script......what am I doing wrong ?
:>
:>Thanks for the help - I'll tell you if I win  ;-)
:>
:>David
:>
:>foreach $r (1..11) {


:>    $i = 0;
:>while ($i < 6) {
:>



:>$i++;
:>}

:>print "$numbers\n";
:>}
:>
:>--
:>
:>______________
:>David J. Evans
:>AMS, The University of Reading, Whiteknights, P.O. Box 228, READING, UK
:>RG6 6AJ
:>Voice : 44 (0)1734 318893  Fax : 44 (0)1734 316537  
:>http://www.reading.ac.uk/Virology
:>
:>

As I understand it Perl calls the standard rand() and srand() functions
which are pseudo-random generators.  If you seed the sequence with:
srand(timeofday) or something similar, you can scramble the sequence to
some extent--but it will eventually _repeat_.  Thoroughly non-random!

And don't do this:  srand(rand());  This, as I understand it, actually
makes the sequence _less_ random ( or shortens the period ).

Quote:

>>>  ...On the net or on your knees ...               >>>

>>>  Tully Steward, SE, [Team OS/2]                   >>>





Mon, 04 May 1998 03:00:00 GMT  
 Is rand() really rand ?

Quote:

>   As I understand it Perl calls the standard rand() and srand() functions
>   which are pseudo-random generators.  If you seed the sequence with:
>   srand(timeofday) or something similar, you can scramble the sequence to
>   some extent--but it will eventually _repeat_.

Well, no duh.  The computer is a finite machine.  If you leave the
computer alone, its entire state will eventually repeat.  The only way
around that is to have some external source of random numbers that
comes from something much more complicated than a computer.  The usual
example is a geiger counter device that's measuring the background
radiation; current physical theories suggest that this is random even
in principle.

`rand' only produces 32 bits of output.  In typical implementations,
each value depends only on the previous value.  The code typically
looks something like this:

        static int rand_seed;

        int rand(void) {
           unsigned long t = rand_seed * something + something_else;
           randno = t % INT_MAX;
           return rand_seed;
        }

        void srand(unsigned s) {
            rand_seed = s;
        }

(The `perl' version of `rand' maps this [0 .. 2^32) range of integers
onto a [0, 1) range of fractions, probably by dividing everything by
2^32.)

The value that `rand' produces depends only on the value that it
produced last time you called it (or the seed you gave to srand, which
is the same thing).  That means that there is a fixed sequence of
numbers, and rand will always give you the numbers from the sequence
one at a time, in order.  When it gets to the end, it starts over at
the beginning.  All `srand' does is tell `rand' where in the sequence
to start giving you numbers.

The sequence can contain at most 2^32 different values, because that's
all the ints that there are.  Therefore, `rand's period is at most 2^32.

Quote:
>   And don't do this: srand(rand()); This, as I understand it,
>   actually makes the sequence _less_ random (or shortens the period).

The `rand' call comes before the `srand', and so `rand' gets called
before you seed it, and so that `rand' call will produce the same
value every time you run your program.  That's the value that you're
seeding with ,and if the seed is the same every time, the sequence of
numbers you get frmo `rand' will be too.  So having

        srand(rand())

at the beginning of your program isn't any more useful than

        srand(1)

.  Since `srand(1)' is always there whether you put it in or not, you
might as well leave it out.

If you meant that you would do `srand(rand())' after *every* call to
rand, it is likely to reduce the period.  `srand(rand)' never does
anything at all.  It sets the seed to whatever random number is
produced by `rand'; but `rand' itself just did this anyway, so the
seed was already set before you called `srand'.  `srand' just set the
seed to whatever it was already.  So this line has no effect; you're
calling `rand' and throwing the result away.

But if you always call `rand' twice, use the first value, and throw
the second one away, you're only getting half of the values in the
sequence.  You're getting the first, third, etc., and throwing away
the second, fourth, etc.  If `rand()' had an even period, like 2^32,
you've just cut that period in half, to 2^31.  If rand had an odd
period, you'll still get all the numbers, just in a different order.

Quote:
> Thoroughly non-random!

The big deal about `rand' is not that it is `random', in the sense of
being unpredictable.  Since `rand' is a computer program, anyone
possessing the source code will find it predictable; this is true of
any possible implementation of `rand' that doesn't depend on measuring
the background radiation or something.  If you were surprised to learn
that the values from `rand' would eventually repeat, then you think
that computers are magical, and you need to forget this, since you
work for a software company.

The big deal aboyt `rand' it is `random' in the sense of being
statistically well-distributed.  A short subsequence (say, of length
2**16) of the values it produces are likely to be statistically
well-behaved.  For example, about half of them will be even numbers.
About half will be smaller than 2**31.  Abuot one-fourth will be even
numbers smaller than 2**31.  One thing that this implies is that if
you do this silly thing of srand(rand()) after every rand(), so that
you're only getting every other random number, it won't matter too
terribly.

(A peripheral note of possible interest: Knuth points out (I think
it's an exercise in volume 2 of _The Art of Computer Programming_)
that the geiger counter test may *not* have this statistical niceness
property.  (!!!))

(Another note:  I pulled the 2**16 in the last paragraph out of my
hat.  There is a number to use here, but I dno't know what it is; I
suspect that it is at least 2**16.)

--

  The night is pleasing to us because, like memory, it erases idle details.



Tue, 05 May 1998 03:00:00 GMT  
 Is rand() really rand ?


Quote:
>As part of my never ending quest for financial independence I have
>written a short script to pick my lottery numbers - six integers, chosen
>from the numbers 1-49, no duplicates.  I have yet to win !

Duh!

Quote:
>However, I've noticed that each time I call the script it returns the
>same numbers.....though these are dependent upon whether I'm using
>MacPerl or the NT port.

Hey, you're all set: no changes necessary.  The probabilty that the
lottery's pick will match your pick is the same whether you pick a
random number or the same number every time.  (Proof at 11:00.)

Quote:
>Here's the relevant part of the script......what am I doing wrong ?

a) failing to seed the [pseudo-]random number generator.
b) wasting memory.  (Not important for 1..49, but your pre-allocating
   an unchanging array containing 1..49 offends my sense of programming
   aesthetics since it won't scale up.)
c) throwing away money at sucker's odds.
d) wasting the time of the good people of this newsgroup.
e) failing to RTFM.

If this reads like a mild flame, well, good.  The perl [5.0] manual and
the Camel book both refer to srand in the their description of rand.
They don't explain pseudo random number generators or the concept
of seeding, so if you'd said "I don't understand what srand does"
I'd have been much more forgiving.  As it is, it's clear that you're
trying to use this newsgroup as a substitute for a manual and a little
experimentation.

Take a look at Tom Christiansen's comments in the "Why don't my questions
get answered" thread in this newsgroup.  (Yes, he has much more cause
to complain than I, but I did do a lot of work porting perl 4 to MS-DOS
so I'm not *just* a consumer here.)
--
Len Reed

Holos Software, Inc.
Voice: (770) 496-1358 ext. 16



Tue, 05 May 1998 03:00:00 GMT  
 Is rand() really rand ?

Quote:


><Invective deleted>

>> If this reads like a mild flame, well, good.  The perl [5.0] manual and
>> the Camel book both refer to srand in the their description of rand.
>> They don't explain pseudo random number generators or the concept
>> of seeding, so if you'd said "I don't understand what srand does"
>> I'd have been much more forgiving.  As it is, it's clear that you're
>> trying to use this newsgroup as a substitute for a manual and a little
>> experimentation.

>Mr. Reed, are you always this {*filter*}? Or did someone{*filter*}in your corn
>flakes this morning?

No, I'm not always this {*filter*}.  Are you?

Quote:
>Some of us who are not up there with you simply want a little bit of
>direction. I, like the previous poster (possibly), *have* read the f******
>manual.

You speculate that the previous poster read the manual, but you provide
no support for that.  I at least backed up my conclusion, saying that
he made no reference to srand().

Quote:
>I've read the whole thing from beginning to end and some sections
>over and over and over. But I find that I always pick up something new,

Me too.  I've read it.  When I work in perl there I have the manual
in front of me.  I don't post questions, though, until I've made an
effort with the manual and the program.

Quote:
>Other times, I look for
>the answer in the manual and I miss it....so I ask here.

Yup, me too.  I contend that it's real hard to miss srand in a four
line description of rand.  So hard, in fact, that I concluded that
the original poster had not even bothered to look at the manual
before posting.

Quote:
>Sorry we're not as brilliant and well-versed in Perl as the great Len Reed.

If you had read my posting, you would see that my problem was not with
the guy's *level* of understanding perl, but rather with his apparent
laziness and desire to get the newsgroup members to do his work for him.
(Okay, I took the occasion to ridicule the lottery, too.) People at my
level can answer beginner and intermediate questions but sometimes need
the help of the real experts (Tom, Larry, etc.).

There are cases where someone can misread the manual or get confused even
when the manual is clear.  Sometimes it isn't clear.  Sometimes the
answer is so obvious that you can feel stupid after your question's
been answered.  All of this is forgivable and should {*filter*} tolerance
from the USENET population.  But failing to read the manual, let alone
the FAQs, is saying, "Please do my work for me."
--
Len Reed

Holos Software, Inc.
Voice: (770) 496-1358 ext. 16



Fri, 08 May 1998 03:00:00 GMT  
 Is rand() really rand ?

    David> As part of my never ending quest for financial independence
    David> I have written a short script to pick my lottery numbers -
    David> six integers, chosen from the numbers 1-49, no duplicates.
    David> I have yet to win !

    David> However, I've noticed that each time I call the script it
    David> returns the same numbers.....though these are dependent
    David> upon whether I'm using MacPerl or the NT port.

I think that your script is trying to tell you something.  You have an
equal chance of getting lucky if you use the same number or different
ones each time, so it doesn't bother going to all the trouble of
thinking up different ones each time you call it.

Isn't perl wonderful - it comes up with the correct solution, even if
it wasn't what you intended!

:-)



Sat, 09 May 1998 03:00:00 GMT  
 Is rand() really rand ?

Quote:


>Well, no duh.  The computer is a finite machine.  If you leave the
>computer alone, its entire state will eventually repeat.  The only way
>around that is to have some external source of random numbers that
>comes from something much more complicated than a computer.  The usual
>example is a geiger counter device that's measuring the background
>radiation; current physical theories suggest that this is random even
>in principle.

Minor historical addition:  another good way of getting really random
numbers is the way they *used* to generate noise in music
synthesizers: by amplifying transistor junction noise.  As I recall
the circuit (been quite a while), they run a cheap and dirty
transistor as a diode, feeding its output into an op-amp with tons of
gain.  What you get out is, if memory doesn't play me false, white
noise.  With filtering you can get pink noise (or other frequency
distributions), thus kiltering the distribution of the random numbers.
This method is much cheaper and easier than the geiger counter trick.
I've always wished machine makers would spend the extra $.50 and put
one in the chassis with an appropriate OS API call.  Then there'd be
no need for pseudorandom generators.

The way you suggest doing it is the one Rudy Rucker uses for his
robots in "Wetware," and the other books in that series.  Their
programming requires them to spend an hour once a year standing in
front of a powerful radiation source, to induce mutations in their
neural pathways (a la Roger Penrose, I'm guessing), preventing them
from becoming totally deterministic (Skinnerian? <g>) and going stale.

Anyway, I digress.  Usually.

Reid Sweatman
University of Utah Computer Center



Sat, 09 May 1998 03:00:00 GMT  
 Is rand() really rand ?


Quote:
>As part of my never ending quest for financial independence I have
>written a short script to pick my lottery numbers - six integers, chosen
>from the numbers 1-49, no duplicates.  I have yet to win !

>However, I've noticed that each time I call the script it returns the
>same numbers.....though these are dependent upon whether I'm using
>MacPerl or the NT port.

>Here's the relevant part of the script......what am I doing wrong ?

>Thanks for the help - I'll tell you if I win  ;-)

>David

>foreach $r (1..11) {


>    $i = 0;
>while ($i < 6) {




>$i++;
>}

>print "$numbers\n";
>}

you must seed the random number generator. A combination of the pid and time
is best, so stick this at the start:

        srand(time|$$);

BTW, here is another lottery number chooser that prevents duplicates:

while ($#chosen < 6) {
    $newnum=int(rand(50)+1);

Quote:
}


Liam
--
 Liam Relihan,             PGP aware - see homepage     Voice: +353-61-335322
 Piercom Ltd., International   [     space    ]           Fax: +353-61-335051

 Tech. Park, Limerick, Ireland   http://itdsrv1.ul.ie/PERSONNEL/lrelihan.html



Sun, 10 May 1998 03:00:00 GMT  
 Is rand() really rand ?


: >

: >
: ><Invective deleted>
: >
: >> If this reads like a mild flame, well, good.  The perl [5.0] manual and
: >> the Camel book both refer to srand in the their description of rand.
: >> They don't explain pseudo random number generators or the concept
: >> of seeding, so if you'd said "I don't understand what srand does"
: >> I'd have been much more forgiving.  As it is, it's clear that you're
: >> trying to use this newsgroup as a substitute for a manual and a little
: >> experimentation.
: >
: >Mr. Reed, are you always this {*filter*}? Or did someone{*filter*}in your corn
: >flakes this morning?

: If you had read my posting, you would see that my problem was not with
: the guy's *level* of understanding perl, but rather with his apparent
: laziness and desire to get the newsgroup members to do his work for him.
: (Okay, I took the occasion to ridicule the lottery, too.) People at my
: level can answer beginner and intermediate questions but sometimes need
: the help of the real experts (Tom, Larry, etc.).

: There are cases where someone can misread the manual or get confused even
: when the manual is clear.  Sometimes it isn't clear.  Sometimes the
: answer is so obvious that you can feel stupid after your question's
: been answered.  All of this is forgivable and should {*filter*} tolerance
: from the USENET population.  But failing to read the manual, let alone
: the FAQs, is saying, "Please do my work for me."

I've saved this post from Larry Wall for inclusion whenever there
appears to be _no_ effort made to solve your own problem before posting.
He's talking about a different newsgroup, but it applies here too:


<Larry>  [snip]  I view a programming language as a place to be
<Larry>  explored, like Disneyland. You don't need to have a lot of preparation
<Larry>  to explore a theme park.  You do have to go along with the crowd
<Larry>  control measures, though.  In a sense, each ride has its own
<Larry>  prerequisites--if you cut in line, you risk getting tossed out of the
<Larry>  park.
<Larry>
<Larry>  What we have here in this newsgroup is a failure in crowd control.
<Larry>  Reading the FAQ is like staying in line--it's something you should
<Larry>  learn in kindergarten.  Usenet needs a better kindergarten.

--
  Tad McClellan,      Logistics Specialist (IETMs and SGML guy)

  Money isn't everything ...
  but it's way ahead of whatever's in second place.



Sun, 10 May 1998 03:00:00 GMT  
 Is rand() really rand ?
Tad McClennan wrote :

Quote:
>I've saved this post from Larry Wall for inclusion whenever there
>appears to be _no_ effort made to solve your own problem before posting.

This posting went considerably further than I had originally meant it to.  
I received several helpful and polite responses for which I am very
grateful.  I _had_ made some effort to solve my own problem - by
purchasing the Llama book several weeks ago....I've looked hard, but can
find no mention of srand() in it.  The application of rand() was trivial
and irrelevant to the question - much like some peoples .sigs it's
included in the mailing, but NOT the purpose of the mailing - it was a
shame that some chose to critiscise both the application and the
question.

I've now bought the Camel book....but I can't guarantee I won't ask what
to others may be obvious questions in the future.  

I may also do some pretty daft programming.......

David

--

______________
David J. Evans
AMS, The University of Reading, Whiteknights, P.O. Box 228, READING, UK
RG6 6AJ
Voice : 44 (0)1734 318893  Fax : 44 (0)1734 316537  
http://www.reading.ac.uk/Virology



Tue, 12 May 1998 03:00:00 GMT  
 Is rand() really rand ?

Quote:

> you must seed the random number generator. A combination of the pid and time
> is best, so stick this at the start:

>    srand(time|$$);

Actually, that's the Most Propagated Perl Typo. You really want to use ^
instead of |.

But it's overkill in every application unless the guys in black hats are
going to try to figure out your random numbers and cause you grief. Using
srand with no arguments is sufficient for almost all of us.

-- Tom Phoenix            http://www.teleport.com/~rootbeer/            




Tue, 12 May 1998 03:00:00 GMT  
 Is rand() really rand ?
I was developing a script that had a neeed for randomness in it. I called
srand first and then, just to test how random it was i printed the product
of int(rand(3)).

The script seemed to nearly always produce the same output each time it
was run. I wrote a script that output the same thing 250 times. These
values did appear to be random. I am using perl on an IBM PC running
FreeBSD but I have noticed this in other languages (under DOS) on IBMs. I
have tried a quick test using HyperCard on a mac and that seemed to
produce random output although i didn't graph it to check or anything.

But having said that how do I get a random integer using perl? I am
thinking I may have to resort to a cron job thayt outputs random numbers
to a file and just et my scripts to iterate through the file...

While Im at it is there any easy way to read in the nth line of a file?

Andrew

--
--
Life is very short, and there's no time
For fussing and fighting, my friend.   WCWIO??TB



Wed, 13 May 1998 03:00:00 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Rand (or not to Rand)

2. rand() not really random?

3. rand not really random

4. platform-independent rand?

5. perl5: int(rand()) doesn't yield integer

6. rand function on Solaris 2.3 doesn't work

7. rand() on MS-DOS

8. rand() doesn't work!

9. rand problem

10. use of rand and srand

11. SUMMARY: Reinitializing rand()

12. Reinitializing rand()

 

 
Powered by phpBB® Forum Software