January 13th, 2011, 05:14 AM
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
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
No need to over complicate things.
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
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.
Originally Posted by Chris_F
You may notice that using Random causes you to get predictable patterns of results.
January 14th, 2011, 04:24 PM
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().
That's not true. The Random class is specified to provide an even distribution of values within a range.
Originally Posted by CreganTur
January 15th, 2011, 02:15 AM
I created a card game and had better results getting a "more" random number this way:
Random randGen = new Random();
int randomCard = randGen.Next() % 13; // 0 base
January 15th, 2011, 03:31 PM
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...
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
January 15th, 2011, 03:40 PM
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:46 PM
Are you sure you had better results? Maybe it just seemed like it at the time you tested.
Originally Posted by SpecialK
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 16th, 2011, 01:48 PM
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.
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.
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!