OT: Random Numbers

Jan 18, 2018 15 Replies
Saved reading
Jump to replies (15)

I am interested in the game Yahtzee - so I decided to emulate it in Excel VBA. A Yahtzee is all of five dice throws giving the same number.



I wrote a VBA program which:



Ran for a million times. Each run generated 5 random numbers between 1 and 6 inclusive. (The 5 dice throws). I used the Randomize function immediately before the call of RND. I recorded results in a spread sheet - number of runs and number of Yahtzees in those runs ie how frequent was there a Yahtzee I visibly noticed that the results were not random at all. First seven Yahtzees found were Yahtzee of sixes. Next a large group of Yahtzees in threes - and other repeating patters, I put a single debug.print statement in the code.



The result was that the program took much, much longer to run : and there was no obvious patterns in the results (I have no idea how "random" the actual result was). Moral of the story: beware of producing random numbers in Excel. (I know that no computer generated number will be random : but I certainly did not expect what happened to actually happen. I know that you can download random number sequences which are generated from atmospheric noise and variation: but I bet there are many systems which rely on computer generated random numbers)


Beware of making random numbers all together,What exactly is random? We had this out back in the old days of computers. The randomising function on many computers is a simple counter of something and then its polled. it relies on the granular nature of time divided into small segments and as you say is not really random. In the spectrum for example seed was loaded with a number representing the number of frames displayed then an algorithm was added on top the make it seem more random. Trouble of ten happens of course that if you compile your code you often compile the contents of system locations hard, so every time it runs it starts exactly as it did the first time. I noticed this on a dice program where the first throw in compiled code was always a 4, in interpreted code it was pretty random.

The trouble is we are playing a game of probabilities here and although unlikely what you saw could still be random if the sample was over a large enough set of numbers. Brian

You might be better off NOT using Randomize before each Rnd, as it uses the System Timer. My Excel 2003 Help says: Randomize uses number to initialize the Rnd function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value.

If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.

The first thing to check is that you do generate a moderately uniform distribution of numbers and there are no fence post errors. It is easy to end up with half a bin at top and/or bottom of the range.

The Excel Random number generator has always been of low quality but you need a moderately sophisticated statistical test to show the faults.

formatting link

It still fails standard tests in 2007 I haven't bothered to check since.

formatting link

There are much better random number generators - some of which can be fairly easily implemented in VBA (though perhaps slower than Rnd())

Strangely, I wrote a shell script to do this a few weeks ago. It was to provide input to a dicewords scripts I wanted students to write.

I used /dev/random which is probably a bit better.

Such errors may sometimes be reduced by generating numbers in the range n = 0 to 2^N-1 and trying again if you get an unwanted number - here, use 0 to 7 and repeat until n is not 0 or 7 (use n%7 = 0, maybe). If floating

-point arithmetic is used in the generation, it may be worth ensuring that all possible initial results have the same exponent - so generate in 70-77, take n = n%7, try again if n = 0.

Randomize should be called once, at the beginning of the program or section . That is so well known that I've not yet found it in Wikipedia.

Donald E Knuth :- "Random numbers should not be generated with a method cho sen at random".

There is a OLD book - I have read the foreword - in all the best libraries, and Amazon, and Google Books : "Buy A Million Random Digits with 100, 000 Normal Deviates" by Rand Corporation (ISBN: 9780833030474) from Amazon's Bo ok Store.

The foreword asserts that the million digits were computed and punched onto paper tape, formatted for printing, by a Flexowriter, to go to the book-pr inter. Some of the pages were properly proof-read; all were inspected. Th ere was one visible layout error. It was judged that it was about equally likely that a tape error would give a wrong layout as that it would give a wrong digit - - - therefore, it was probable that the list contained just o ne incorrect digit.

A University computer department was about to run a Business Game course, u sing (IIRC) Algol 60 on an ICT 1900, with the events derived from the Rando m function; they would have used Randomize correctly, initialising from the machine time-of-day clock. I remarked that if the Game was scheduled to s tart running as 09:00 each day, the Gamers should in principle get the same set of random numbers each day. At which the relevant programmers adopted a worried expression and ran off to their programming room ...

Try

formatting link

-randm.htm and related pages via

formatting link
!!

There are many different generators - some are better than others

but I bet there are many systems which rely on computer

formatting link

It still does not address the issue though.What we might suggest is random, is only random at that time. As I said before its a probability game really. I once threw five 6s on a dice in a row on a real non tampered with dice. The sequence if continued long enough would only show the small number of the same throws as being a tiny blip on a completely random distribution, but at the time I was accused of cheating which I was not.

I tell you if you look into the random number subject a bit it will do your brain in.. Brian

Exactly. RND can't produce a pseudo-random sequence if you only ever ask it to generate one value before re-seeding it - that is effectively asking the seed generator to act as the pseudo-random number generator itself.

Doesn't that depend on where the randomise function gets its seed? I assume it's a clock of some sort, e.g. number of 1/100 seconds from turning the machine on.

But if you are generating random numbers in a tight loop that means that you cycle through seed values in a smallish fixed increment (possibly even the same seed value several times). This will introduce additional correlations in the already weak pseudorandom number generator that is implemented in Excel. You call randomize just once at the start.

Even used correctly Excel Rnd performance has been analysed elsewhere and found wanting. It is nowhere near as bad as it was in the early days but it is nothing like state of the art and not a correct implementation of the algorithm that it claims to be based on either :(

It could be worse. The infamous IBM RANDU excels at bad performance.

formatting link

RTC is a pretty good place to get random s*it from

Once. after that its not random any more.

I'd vote wodney for that, he knows where he shovles s*it.

Good point, but its seldom random.

So much so one suspects its a poorly constructed A[I?].

That's why you plug it into the random number generator - though better to store the result as the seed for the next call (if possible).

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required