Excel VBA: Rnd() method isn't random enough

0

0

I'm using Excel and VBA to simulate a large number of dice rolls.

The problem I'm having is after a certain period of time, the rolls start repeating, and I start getting results that are anything but random.

I'm using INT = RND() * 6 +.5 for each of the dice.
Is there any way of making it a couple orders of magnitude more random?
I do use Randomize before hand to prevent starting the same way every time.

tkulogo

Posted 2013-11-14T20:37:39.390

Reputation: 1

1What research have you done so far? How have you established that the sequence is being repeated? What code are you using? – James Snell – 2013-11-14T21:12:07.037

Humans don't have a good feel for true randomness. I wrote a script to select and play a random ep of startrek, but noticed it replaying a few eps too often. I decided to test it by generating a batch of 200 eps, and analyzed the stats. my algorithm was actually 'better' than random because it selected 77% of the possible eps, and never repeated any one more than twice. a truly random algorithm would cover about 67% of the range. in the end I determined that I didn't actually want random, and added a history/blacklisting feature to prevent replays. hope that anecdote helps. – Frank Thomas – 2013-11-14T21:33:16.350

How did you determine the output is "not random enough"? Have you done a chi-squared test? I'm just curious. Also, it would help to see your code. – Excellll – 2013-11-14T21:48:04.383

Do you use Randomize or Randomize Timer like its explained on SO?

– nixda – 2013-11-16T14:57:03.047

Answers

1

VBA probably implements a pseudorandom function. Since it's a function, it repeats, but generates a set of values that has a random-like distribution.

You may need to develop or look for a library that takes a source of randomness and generates a sequence of random numbers based on that. One such source of unpredictability is the time between keypresses, or collecting input such as mouse movements.

You could also use a web service and download a large number of random numbers. random.irb.hr and random.org are two such services.

random.irb.hr has some libraries on their site that allows you to programmatically access the service once you make an account.

You could also look into buying the actual generator they use and using the software they provide with it.

LawrenceC

Posted 2013-11-14T20:37:39.390

Reputation: 63 487