-
January 13th, 2011, 05:14 AM
#1
RandomDice
Hey i am making a Yahtze game and need some help.
I want to write the code to how the dices will work so.
What i've been trying to do is:
I made six variables since there are 6 sides on the Dice.
Then i am going to make a function which rolls randomly one of these variables. So i made this one:
public Int32 RandomRollDice
{
//random generator code here
return "value will be here";
}
But i realize i have no idea how i do this, and i have no idea how to define the return value. I've tried to search internett bu,t my hits are mainly on classes and such things i do not understand. Are there like any simple functions for a random roll i could understand as a beginner?
So if anyone could give me a hint, thanks.
-
January 13th, 2011, 06:12 AM
#2
Re: RandomDice
This is a very good tutorial on arrays: http://www.quack-ware.com/tutorials/Csharp.aspx?id=4
Check it out. It's very clear and easy to follow. It demonstrates the use of arrays to hold and display the results of 2 dice being rolled randomly.
Hope this helps.
-
January 13th, 2011, 06:29 AM
#3
Re: RandomDice
No need to over complicate things.
Code:
Random rnd = new Random();
int value = rnd.Next(1, 6); // do this every time you need a new dice roll value
-
January 14th, 2011, 04:13 PM
#4
Re: RandomDice
Originally Posted by Chris_F
No need to over complicate things.
Code:
Random rnd = new Random();
int value = rnd.Next(1, 6); // do this every time you need a new dice roll value
To offer a little more explination- this uses Random and tells it that you want a value returned between 1 and 6. Just make a call to this for each die object that you're working with to get a new value.
You may notice that using Random causes you to get predictable patterns of results.
-
January 14th, 2011, 04:24 PM
#5
Re: RandomDice
Chris is corret, but for one missing detail. You don't want to create a new random object each time you call .Next(). When you use the default constructor the seed value is taken from the system clock. The system clock has a finite resolution, so if you call this method quickly in succession, many of the Random objects you create will be using the same seed value and thus will return the same result when you call Next(). So, create your Random object once and use it for each call to .Next().
Originally Posted by CreganTur
You may notice that using Random causes you to get predictable patterns of results.
That's not true. The Random class is specified to provide an even distribution of values within a range.
-
January 15th, 2011, 02:15 AM
#6
Re: RandomDice
I created a card game and had better results getting a "more" random number this way:
Code:
Random randGen = new Random();
int randomCard = randGen.Next() % 13; // 0 base
-
January 15th, 2011, 03:31 PM
#7
Re: RandomDice
SpecialK... You maybe correct, but closer inspection (mathematically) of your code would mean that...
Unless Int32.MaxValue was exactly divisible by 13, then there would be slighly more chance of getting lower numbers (ie those <= to the remainder of Int32.MaxValue / 13. All other things being equal, which I am sure they are not
I'm being picky here, obviously Int32.MaxValue is so far greater than 13, it is almost negligible...
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
January 16th, 2011, 01:46 PM
#8
Re: RandomDice
Originally Posted by SpecialK
I created a card game and had better results getting a "more" random number this way:
Are you sure you had better results? Maybe it just seemed like it at the time you tested.
Generally, it's a bad practice to go and make the Random class "more random", and people trying to do that often get some really "fantastic" ideas (you'd be surprised). It's not so bad in your case, but still...
-
January 15th, 2011, 03:40 PM
#9
Re: RandomDice
Yeah, the documentation claims that you will get an even distribution within any range, so unless you have some concrete data and test cases showing that that claim is incorrect I'm going to go ahead and call shenanigans. Just specify the upper bound.
-
January 16th, 2011, 01:48 PM
#10
Re: RandomDice
They're not going to be 'more random' if you do that. The algorithm doesn't change so the 'randomness' is identical whether or not you use "% 13". What you can do to improve the performance is read the third paragraph in the 'Remarks' section here: http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
Also bear in mind that when specifying min/max values in Random.Next, the lower bound is included but the upper bound is not. i.e. random.Next (5, 10) will give you a random integer in the set: 5, 6, 7, 8, 9. So if you want to get random numbers between 1 and 6 (inclusive) you need: random.Next (1, 7). Full documentation is here: http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx
Last edited by Mutant_Fruit; January 16th, 2011 at 01:51 PM.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|