Public Group

# Simple question about simulating a 6 sided dice.

This topic is 3991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
Read the documentation (on MSDN) for the NextDouble function, specifically the "return value" part, and you will have your answer.

##### Share on other sites
Quote:
 Original post by SneftelRead 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.

##### Share on other sites
In this case, wouldn't it just be easier and more straight forward to use .Next(1, 7)?

##### Share on other sites
Quote:
 Original post by InstruoIn 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.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633710
• Total Posts
3013486
×