POV-Ray : Newsgroups : povray.off-topic : Coin game Server Time
6 Sep 2024 15:22:06 EDT (-0400)
  Coin game (Message 20 to 29 of 29)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: scott
Subject: Re: Coin game
Date: 9 Jan 2009 02:50:36
Message: <496701cc$1@news.povray.org>
>> >  How did you implement it? What type of integers did you use to count 
>> > the
>> > amount of money? (Note that a 32-bit integer will overflow after only 
>> > 32
>> > tosses of the coin, which is perfectly within the realm of 
>> > possibility.)
>
>> I used 32bit and then 64bit integers, but the result was the same.
>
>  Did you at least put an assert() to check that no more than 31 (or 63)
> tosses were ever made?

It seems that the number of tosses never exceeds 16, even when running it 
1e9 times (where you would expect around 30 tosses is likely to come up).

OK I just fixed it now, I changed my head/tail generator from:

(rand()%2) == 0

to

rand() <= RAND_MAX/2

and now it seems to behave better, after 1e9 goes the maximum number of 
tosses in a row is 29, and the average winnings per go are around $14.30.

I guess that the RNG doesn't have more than 16 odd or even integers in a 
row, but it does has more than 16 either side of the mid-value.  Perhaps I 
need a better RNG to investigate further.


Post a reply to this message

From: scott
Subject: Re: Coin game
Date: 9 Jan 2009 02:57:34
Message: <4967036e$1@news.povray.org>
>> $10,
>
> It's an even game for 20 throws, favours me for higher. You must show me
> that you have $524,288.

OOC why the 20 and why the $2^19 figues?  That's what I was really 
interested in.


Post a reply to this message

From: scott
Subject: Re: Coin game
Date: 9 Jan 2009 03:00:28
Message: <4967041c$1@news.povray.org>
> Also, I assume that you're familiar with the name of this puzzle, but on 
> the off chance that you're not: 
> http://en.wikipedia.org/wiki/St._Petersburg_paradox

No I wasn't aware of the name (so as usual it is then very hard to find any 
info about it), thanks!


Post a reply to this message

From: Warp
Subject: Re: Coin game
Date: 9 Jan 2009 04:06:26
Message: <49671392@news.povray.org>
scott <sco### [at] scottcom> wrote:
> (rand()%2) == 0

> to

> rand() <= RAND_MAX/2

  The rand() function in most compilers is of extremely poor quality and
should never be used for this type of application. You should always use
a very high-quality RNG with a humongous period.

  I used (my C++ version of) the ISAAC RNG, which should be of fairly high
quality.

-- 
                                                          - Warp


Post a reply to this message

From: Invisible
Subject: Re: Coin game
Date: 9 Jan 2009 04:15:49
Message: <496715c5$1@news.povray.org>
Warp wrote:

>   The rand() function in most compilers is of extremely poor quality and
> should never be used for this type of application. You should always use
> a very high-quality RNG with a humongous period.
> 
>   I used (my C++ version of) the ISAAC RNG, which should be of fairly high
> quality.

I understand the Mersenne Twister is designed specifically for these 
kinds of simulations - but I don't know how widely available it is...

PS. One time I wrote my own PRNG just to generate some random data for 
testing purposes. I don't know why I didn't use the PRNG library, but I 
should have; on investigation, it turns out that my own PRNG almost 
always has a period of 2. (!!) Oops...


Post a reply to this message

From: Warp
Subject: Re: Coin game
Date: 9 Jan 2009 04:52:40
Message: <49671e67@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> I understand the Mersenne Twister is designed specifically for these 
> kinds of simulations - but I don't know how widely available it is...

  The ISAAC RNG is faster than MT. I don't know about quality, but it should
be very competitive at least.

> PS. One time I wrote my own PRNG just to generate some random data for 
> testing purposes. I don't know why I didn't use the PRNG library, but I 
> should have; on investigation, it turns out that my own PRNG almost 
> always has a period of 2. (!!) Oops...

  One of the basic things taught at school is that you should never attempt
to make your own PRNG if you don't have extensive experience about the
subject, if you want any kind of quality to it.

  Another thing with the exact same rule is a hashing function.

-- 
                                                          - Warp


Post a reply to this message

From: Invisible
Subject: Re: Coin game
Date: 9 Jan 2009 05:00:54
Message: <49672056$1@news.povray.org>
>> I understand the Mersenne Twister is designed specifically for these 
>> kinds of simulations - but I don't know how widely available it is...
> 
>   The ISAAC RNG is faster than MT. I don't know about quality, but it should
> be very competitive at least.

Well I seriously doubt it can possibly be any *worse* than an LCG. 
(Otherwise... why would they have designed it?)

>> PS. One time I wrote my own PRNG just to generate some random data for 
>> testing purposes. I don't know why I didn't use the PRNG library, but I 
>> should have; on investigation, it turns out that my own PRNG almost 
>> always has a period of 2. (!!) Oops...
> 
>   One of the basic things taught at school is that you should never attempt
> to make your own PRNG if you don't have extensive experience about the
> subject, if you want any kind of quality to it.
> 
>   Another thing with the exact same rule is a hashing function.

Or, indeed, several other kinds of construct. (E.g., it's notoriously 
hard to make a good encryption algorithm.)

In my defense, in the incident in question "good quality" random numbers 
weren't necessary. I just needed lots of different numbers with no 
particular pattern to them. OTOH, what I got was something like

   3, 11, 3, 11, 3, 11, 3, 11...

which is obviously no use at all. The PRNG generates longer sequences in 
a few cases, but most of the time it does not. And I somehow failed to 
notice this.

In future, I'm going to use the PRNG library. No matter how stupid its 
API is!


Post a reply to this message

From: scott
Subject: Re: Coin game
Date: 9 Jan 2009 09:07:04
Message: <49675a08$1@news.povray.org>
>  I tried it myself with the program at the end of this post, and got a
> bit different of a result. The program printed:

I just found out that Windows includes a better RNG anyway, you call it with 
the API function CryptGenRandom.  More details here:

http://en.wikipedia.org/wiki/CryptGenRandom

It's a fair bit slower than rand(), but by making use of every bit returned 
(rather than just testing the LSB) it is comparable.


Post a reply to this message

From: Kevin Wampler
Subject: Re: Coin game
Date: 9 Jan 2009 12:09:03
Message: <496784af@news.povray.org>
Warp wrote:
>   One of the basic things taught at school is that you should never attempt
> to make your own PRNG if you don't have extensive experience about the
> subject, if you want any kind of quality to it.

This method seems to work pretty well: http://xkcd.com/221/


Post a reply to this message

From: Darren New
Subject: Re: Coin game
Date: 9 Jan 2009 12:51:01
Message: <49678e85$1@news.povray.org>
Warp wrote:
>   One of the basic things taught at school is that you should never attempt
> to make your own PRNG if you don't have extensive experience about the
> subject, if you want any kind of quality to it.

And even if you do. Knuth tells stories of his own attempts while writing 
his semi-numeric methods tome, having thought he came up with a great RNG 
and found it generated a couple hundred numbers before settling into a short 
cycle of something like 3 or 4 values.



-- 
   Darren New, San Diego CA, USA (PST)
   Why is there a chainsaw in DOOM?
   There aren't any trees on Mars.


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.