Simple question about simulating a 6 sided dice.

Started by
3 comments, last by avorpalbunny 16 years, 3 months ago
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.
Brett Biery2D/3D Artist and Graphic DesignNewbie Programmer
Advertisement
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.
Brett Biery2D/3D Artist and Graphic DesignNewbie Programmer
In this case, wouldn't it just be easier and more straight forward to use .Next(1, 7)?
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
Quote:Original post by Instruo
In this case, wouldn't it just be easier and more straight forward to use .Next(1, 7)?


I'll look at that and see what it does, im pretty much a newb at this point.
Brett Biery2D/3D Artist and Graphic DesignNewbie Programmer

This topic is closed to new replies.

Advertisement