Simple question about simulating a 6 sided dice.
I'm reading through a C# book about creating random numbers as they apply to dice and something occurred to me.
The code they use is:
die = (int)(generator.NextDouble() * 6) + 1;
The computer randomly finds a number 0 to 1 and then * 6 and adds 1 so that the number doesn't end up being 0. It later cuts off the decimal information to get the number used for the dice.
What happens if it randomly generates a 1 by chance though? 1 * 6 + 1 = 7
I suppose you could write some if statement to make 7 to 6, but is there any easier way and why is this overlooked in the book?
Thanks for the help.
Read the documentation (on MSDN) for the NextDouble function, specifically the "return value" part, and you will have your answer.
Quote:Original post by Sneftel
Read the documentation (on MSDN) for the NextDouble function, specifically the "return value" part, and you will have your answer.
Great, I found my solution. I didn't know they had a MSDN site that explains all the functions. That should help with questions I have later too. Thanks alot.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement