09-09-2019 12:20 PM - edited 09-09-2019 12:20 PM
@RTSLVU wrote:
Here's how I simulated rolling a die with animation
NO NO NO.
The correct implementation of a random number generator is to multiply by the number of different things you want and round DOWN. So mutltiply by 6, and round down will give you a number between 0 and 5 each with an equal chance of occurring. Since this is a die, you will add 1 to the result to give you a number from 1 to 6.
Look at the difference in distribution of the numbers. With the image in this post, the numbers 1 and 6 only happen about half as often as they should.
09-09-2019 12:21 PM
@crossrulz wrote:
If you are using LabVIEW 2019, just use the Random Number (Range).vi.
EDIT: You will want to use the U64 or I64 version, not the DBL.
This is cool. It may be time to install 2019.
09-09-2019 12:23 PM
@RavensFan wrote:
NO NO NO.
The correct implementation of a random number generator is to multiply by the number of different things you want and round DOWN. So mutltiply by 6, and round down will give you a number between 0 and 5 each with an equal chance of occurring. Since this is a die, you will add 1 to the result to give you a number from 1 to 6.
Look at the difference in distribution of the numbers. With the image in this post, the numbers 1 and 6 only happen about half as often as they should.
Ah well I was mainly trying to show off my animation, I never really thought about the random number generation.
09-09-2019 12:27 PM - edited 09-09-2019 02:07 PM
@REFNH wrote:
if we want that the probability is the same for all the numbers we can use a while loop
A while loop does not generate random numbers and your code still gives unequal probabilities. Did you even test?
(You could fix the probability issues by multiplying by 6.5 instead of six, but there is no justification for such convoluted code if better alternatives exist. See below.)
09-09-2019 01:23 PM - edited 09-09-2019 01:50 PM
While the help states that 0 is a possible outcome, this is actually incorrect as has been discussed elsewhere.
So, to get numbers 1..6, all we need to do is round towards +inf. No need for the +1.
As you can see, the code that was incorrectly marked as solution (above) still only give half the probability (or even less chance to get boxcars with two dice! You do the math!) With code like that, you'll never get a job in Vegas (top code in picture) :D.
Here's all we need for a fair dice (bottom code):