RandomDice
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: RandomDice

  1. #1
    Join Date
    Oct 2010
    Posts
    15

    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.

  2. #2
    Join Date
    Nov 2010
    Location
    England
    Posts
    21

    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.

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    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

  4. #4
    Join Date
    Dec 2008
    Posts
    144

    Re: RandomDice

    Quote Originally Posted by Chris_F View Post
    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.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    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().

    Quote Originally Posted by CreganTur View Post
    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.

  6. #6
    Join Date
    Jan 2011
    Posts
    11

    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

  7. #7
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  9. #9
    Join Date
    Jan 2010
    Posts
    1,102

    Re: RandomDice

    Quote Originally Posted by SpecialK View Post
    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...

  10. #10
    Join Date
    May 2007
    Posts
    1,546

    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 "&#37; 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center