MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: The two dices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg54524] Re: [mg54486] The two dices
  • From: "Dr A.H. Harker" <a.harker at ucl.ac.uk>
  • Date: Tue, 22 Feb 2005 04:23:52 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

I thought I had an answer to this, but now I realise I don't understand it.

  This is the sort of problem that can occur in any programming language
with a function that has hidden side-effects. Random[] can only work by
squirreling away in memory a number to which it is going to apply an
algorithm in order to generate the next pseudo-random number in the sequence
(this number is the variable $RandomState). What you have to do is to give
Mathematica some clue that this is going on in the background, otherwise
when it comes to try to optimise things one call of dice[] looks as if it
should be exactly another. Something like Update[] wrapped around the call
would be nice, but Update[] only works with Symbols, and dice[] cannot be
defined as a Symbol. One way I can see is

 theThrows = Table[Block[{}, dice[]] + Block[{}, dice[]], {n}];
(note, though, that you do NOT want to use Block[{$RandomState},dice[]]),

 but there may be other ways of signalling to Mathematica that the calls to
dice[], apparently with identical arguments, produce different results. For
example, if you define
 dice[___] := Random[Integer, {1, 6}]
 you can still call it with no arguments, but it is handled correctly. A
look with Trace shows that the evaluation sequence is different with dice[]
and dice[___].

 There are two problems with the above argument. One is that Mathematica
does not have any problem if one uses 
 theThrows = Table[Random[Integer, {1, 6}] + Random[Integer, {1, 6}], {n}];
and the other is that it does not have any problem with 
 i = 1
dd[] := i = i + 1
Table[dd[] + dd[], {i, 1, n}]
 which one would have expected to fall victim to the same difficulty.

 Where is the problem then? It's not a matter of compiling (does Table
switch on compilation when the number of iterations exceeds some critical
number, such as 249?), as Compile[{},dice[]+dice[]] produces compiled code
that generates even and odd results.

 Tony Harker 
 Department of Physics and Astronomy
 University College London
 Gower Street
 LONDON
 WC1E  6BT
 (44)(0)207 679 3404
 a.harker at ucl.ac.uk


]->-----Original Message-----
]->From: Antoine [mailto:bugaboo12703 at yahoo.fr]
To: mathgroup at smc.vnet.net
]->Sent: 21 February 2005 08:45
]->To: mathgroup at smc.vnet.net
]->Subject: [mg54524] [mg54486] The two dices
]->
]->Hello,
]->
]->I wanted to simulate the throws of two dices and something happened I
]->can't
]->explain:
]->(Mathematica 5.0 , the same occurs on Mathematica 5.1 )
]->
]->
]->The following code is OK:
]->
]->
]->n=300;
]->theThrows = Table[Random[Integer, {1, 6}] + Random[Integer, {1, 6}],
]->{n}];
]->theStats = Table[Count[theThrows, i], {i, 2, 12}]
]->
]->
]->the occurences, as n becomes greater and greater, looks like the Gaussian
]->curve
]->(sorry for my English)
]->
]->
]->Suppose you want your dice function, and that it is defined by
]->
]->
]->dice[]:=Random[Integer, {1, 6}]  ( nothing wrong about it, I suppose )
]->
]->
]->Now the behavior of the following code changes depending on the value of
]->n.
]->If n < 250 everything is fine ...
]->hen n >= 250, then there is no occurence of odd values.
]->( the two dices never give 3, 5, ....)
]->
]->
]->n=250;
]->theThrows = Table[dice[] + dice[], {n}];
]->theStats = Table[Count[theThrows, i], {i, 2, 12}]
]->
]->
]->Does a rationnal explanation exists ?  Is it a bug ?
]->
]->
]->The two definitions ( see below ) of the dice function "makes" the code
]->running properly.
]->But, of course, I prefer the former definition.
]->
]->
]->dice:=Random[Integer, {1, 6}]  ( not so nice, specially if you want to
]->have
]->several dice functions)
]->dice[_]:=Random[Integer, {1, 6}]   ( you must give a parameter to the
]->function, quite horrible )
]->
]->
]->Thank you for your help.
]->Antoine
]->



  • Prev by Date: Re: Leading Zeros? (question rephrased)
  • Next by Date: Re: The two dices
  • Previous by thread: Re: The two dices
  • Next by thread: Re: The two dices