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?
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.
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.
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.......
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.
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...
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 12:51 PM.
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.
Bookmarks