Reputation: 59
I need simulate classic play cube (values 1-6) by Rnd function. My solution is Round(5 * Rnd() + 1, 0)
, but when I generate 1 milion values, I have not good results (by picture).
I think, that problem is in bad use of ROUND function, but I do not know where.
Thank you for your answers...
Upvotes: 1
Views: 216
Reputation: 1192
Let's look at this piece-by-piece.
Rnd()
gives a number between 0 and 1.
So 5 * Rnd() + 1
gives a number between 1 and 6.
We can think of this number as being equally likely to be in one of 10 half-integer areas: [1,1.5], [1.5,2]...[5.5,6].
One of these will round to 1 - [1,1.5].
Two of these will round to 2 - [1.5,2] and [2,2.5].
3, 4 and 5 similarly have two of these which will round to them.
One of these will round to 6 - [5.5,6]
This explains the distribution you're seeing.
Therefore Round(6 * Rnd() + 0.5, 0)
should give you what you're looking for as it extends the area by half in both directions, so gives 1 and 6 the correct weightings.
Upvotes: 5