Post Reply 
little math/programming problems April 2023
05-03-2023, 12:18 PM (This post was last modified: 05-04-2023 12:13 PM by Albert Chan.)
Post: #7
RE: little math/programming problems April 2023
(05-02-2023 08:03 PM)David Hayden Wrote:  Does anyone spot a problem? Maybe this is a case of rand() showing it's limitations?

rand(), and many other random number generators, have weaker random low bits.
We should use random high bits instead.

< int side = sides[rand() % 6];
> int side = sides[rand() * 6ull / (RANDMAX+1u)];

Bonus: (RAND_MAX+1u) is likely pow-of-2, division should compiled as fast bits shift.
Note: we can speed up a bit more, replacing 6ull by 6u, if it does not cause overflow.

I have ignored slight bias mapping rand() to 0 .. 5
see http://www.azillionmonkeys.com/qed/random.html
see https://lemire.me/blog/2019/06/06/nearly...s-systems/

However, because of long tails, it is hard to get good estimate, even with good random numbers.
For 5 dices, correct expected value = 5.491 819 ...
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: little math/programming problems April 2023 - Albert Chan - 05-03-2023 12:18 PM



User(s) browsing this thread: 1 Guest(s)