Is there an API, library, or some other chip-set based command I can access in VBA?
I currently have a setup for getting random numbers; however, when I put the result sets to the test, the numbers do not even come close to generating a good statistical curve. I tested this by generating 600 simulated rolls of 2 six-sided dice totaling the 2 dice together each time. I was hoping for the number 7 to take a huge lead; however it came in second twice with nowhere near the appropriate statistical curve being created.
My current code uses the standard VBA method, but as I said fails the statistics test:
Randomize
GetRandomNo = Int((6 * RND) + 1)
For a rather full answer:
There are numerous ways to generate random numbers, but one way is to use the Windows API to do the heavy lifting. Windows has API functions to generate cryptographically secure random bytes, and these functions can make use of hardware random number providers.
First, we declare the API functions:
Then, we use this call, and use modulus to reduce our number to one in the desired range:
This approach is fine if you assume an integer has an infinite range of values. However, it hasn't, which means at the limits it's imprecise. Similarly, multiplication assumes an infinitely precise number, which also isn't true and causes a slight bias.
We can get around this by directly using the binary presentation of numbers, and discarding numbers that fall outside of this template:
Now, let's investigate the performance of your solution and both opportunities for rolling dice: I've simulated 100000 random numbers for each approach between 1 and 6.
This is the result:
While the first approach seems to have larger variance between numbers (specifically less ones and more twos), for most applications I'd assume the first is accurate enough.