Re: random number geneator
From: Pierre Espenan (whistling_rabbit_at_yahoo.com)
Date: 01/09/04
- Next message: Peter Koch Larsen: "Re: STL speed"
- Previous message: tom_usenet: "Re: STL speed"
- In reply to: Karl Heinz Buchegger: "Re: random number geneator"
- Next in thread: Sonoman: "Thank you all for the help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 9 Jan 2004 01:45:09 -0800
"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:3FFE71D6.BDDB1187@gascad.at...
> Pierre Espenan wrote:
> >
> > "Chris Theis" <Christian.Theis@nospam.cern.ch> wrote in message
> > news:yv9Lb.11834$Tz1.2967@news.chello.at...
> > >
> > > "Pierre Espenan" <whistling_rabbit@yahoo.com> wrote in message
> > > news:zi6Lb.44604$Pg1.28552@newsread1.news.pas.earthlink.net...
> > > >
> > > [SNIP]
> > > > >
> > > > > for (j = 1; j <= tests; j++)
> > > > > {
> > > > > while (match != 1)
> > > >
> > > > at the begining of the loop add,
> > > > "srand(clock());"
> > >
> > > This advice is a bad, bad idea regarding randomness. srand should be
> > called
> > > only once(!!) in a program and NEVER inside a loop. The reason is that
> > > calling srand once will yield in a start seed that defines the
sequence of
> > > random numbers produced. After calling rand() the internal seed is
updated
> > > so that the next call or rand() will result in the next random number
of
> > > this specific sequence. If you call srand() more often you are going
to
> > jump
> > > from sequence to sequence which will certainly destroy randomness.
> > Although
> > > for this case it might not be a real problem you should refrain from
> > > multiple calls to srand, unless you know exactly what you are doing
and
> > what
> > > the results are.
> >
> > Did you actually try to compile and run the code listed?
>
> Did you ever read the documentation for rand()?
> If you call rand(), it will also update the seed value. Thus when calling
> rand() the next time, it will generate a different random number (and
> update the seed value again) such that when calling rand() the next time
> a different random value is generated (and the seed is updated again) etc.
>
> It is important to let rand() do this in its own, because randomness in
> a computer is a statistical property. If you reseed the random number
> generator to often, this statistical property cannot build up (as with
> all statistics, it works only for a sufficient large number of samples,
> in this case a sufficient large number of random values).
>
> > Did you see what
> > he is talking about? The same number is generated by rand() in each
loop
> > because the loop has a constant relationship to clock().
>
> It doesn't matter. The main problem is, that srand() should be called only
> once (!) in a program, not often, especially not before every call to
rand()!
> BTW: The original code didn't have a call to srand() at all. Thus there
could
> not have been any realtionship to clock() (which was also not in the
original
> code).
>
> Common practice is to not use clock() for seeding the random number
generator
> but using time() to do that.
Oops! I tried srand(time(NULL)) outside of the loop and this does yield the
correct result without cin.get() so I guess this is the source of all the
problems
generating (pseudo) random numbers I have been having. I just confused
clock()
with time().
>
> > Puting in
> > cin.get() into the loop insures that a different seed is generated each
> > loop.
>
> And how does this help in a program which should run on it's own without
> a user?
>
> > I ran the modified program for 100 loops and the average for count
> > came out to 24 which is very close to the average expected by strict
> > mathematical analysis. The reason srand(clock()) is discouraged within
a
> > loop is that unless special care is taken a new seed will not be
generated.
>
> The reason why you shouldn't call srand() more then once is, that you
destroy
> the randomness of the sequence if you do it. A random number generator is
> designed to give a *sequence* of random numbers. It is only the sequence
> which shows the properties of randomness. Don't interfere with the
> generation of the sequence or you will destroy randomness very easily.
>
> --
> Karl Heinz Buchegger
> kbuchegg@gascad.at
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
- Next message: Peter Koch Larsen: "Re: STL speed"
- Previous message: tom_usenet: "Re: STL speed"
- In reply to: Karl Heinz Buchegger: "Re: random number geneator"
- Next in thread: Sonoman: "Thank you all for the help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|