Thread: Simulating a dice roll, extreme beginner needs help

  1. #1
    Registered User
    Join Date
    Jan 2014
    Location
    London
    Posts
    1

    Simulating a dice roll, extreme beginner needs help

    I am trying to simulate a random dice roll as a basis for a chutes and ladders game

    Code:
    int main {
    int i = 0, diceroll; 
    while (i>5)
    {
    diceroll = 1+rand()%6;
    
    
    i++;
    }
    
    return 0;
    }


    however the numbers get don't seem to be random, it always starts off with 6,6,5.. then its random.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That's invalid syntax, main is declared incorrectly. You wont get any numbers. Post real code next time.

    EDIT: And make sure it's properly indented and actually readable.

    You need to call srand(), to "seed" the PRNG. Call it once, near the beginning of your program (i.e. before you call rand). A common way to get different random numbers on every roll is to use the current time for the seed, though if you run your program in very quick succession, this doesn't work so well (since time() only reports back in whole seconds, and if you run your program twice within the same second, you get the same seed, and same PRNG sequence). Also, #include the right headers
    Code:
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
        srand(time(NULL));
    Last edited by anduril462; 01-24-2014 at 10:55 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is then the problem that, depending on how srand()/rand() themselves are implemented, that rand()%6 may not give results uniformly distributed between 0 and 5. For example, they may be biased more toward some of the values.

    If you want particular distribution or other properties of your random values, you need to find a generator which supports those properties.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe what the OP doesn't know, is that rand() is SUPPOSED to return the exact same numbers, with every run.

    That is meant for statistical testing and debugging purposes.

    When you are ready for more random values, just seed the random number generator, as explained above.

    And be aware as Grumpy noted, that the values you receive, from a very small range of possible values, will be less than perfectly random - sometimes a LOT less.

    Perfectly random number generation is much trickier than it seems, at first.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Just for fun - as mentioned the particular implementation of rand()/srand() may produce better, or worse, results.

    For 20 samples (absurdly low, I know) I get

    Code:
    Value                  Method 1                  Method 2
        0               8 (0.40000)               7 (0.35000)
        1              12 (0.60000)              13 (0.65000)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define DICE_SIDES      2
    #define NUMBER_ROLLS    20
    
    int main(void)
    {
        unsigned dist1[DICE_SIDES],
                 dist2[DICE_SIDES],
                 i;
               
        /* srand(time(NULL)); */
               
        memset(dist1, 0, sizeof dist1);
        memset(dist2, 0, sizeof dist2);
        
        for (i = 0; i < NUMBER_ROLLS; i++) {
            dist1[rand() % DICE_SIDES]++;
            dist2[(int)((double)rand() / RAND_MAX * DICE_SIDES)]++;
        }
        
        printf("%5s %25s %25s\n", "Value", "Method 1", "Method 2");
        for (i = 0; i < DICE_SIDES; i++)
            printf("%5u %15u (%.5f) %15u (%.5f)\n", i, 
                        dist1[i], dist1[i] / (double)NUMBER_ROLLS,
                        dist2[i], dist2[i] / (double)NUMBER_ROLLS);
        
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Hodor: a die with two sides might be better known as a coin, and will only roll if it lands on neither side.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by grumpy View Post
    Hodor: a die with two sides might be better known as a coin, and will only roll if it lands on neither side.
    It had six sides originally but I stood on it.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by grumpy View Post
    There is then the problem that, depending on how srand()/rand() themselves are implemented, that rand()%6 may not give results uniformly distributed between 0 and 5. For example, they may be biased more toward some of the values.
    In particular, if RAND_MAX is not evenly divisible by 6 (and on any computer you're likely to encounter, that will be true), there will be a bias towards some values. E.g., if you're using the modulus method and if RAND_MAX is 32767 (a depressingly common value), then 6 goes into it 5461 times with 1 remainder. Hence, 1 is 1/5462 (or about 0.000183) more likely than the other values, assuming an even distribution. The multiplication method still has this problem, but the biased value will probably be different.

    And historically there was a problem with the low bits, which makes the modulus method especially problematic.

    Basically, rand() is the poor man's RNG and needs to be replaced for anything serious.

    And of course, there's no such thing as "perfect random number generation" with pseudo-random number generators. You'd need to access quantum effects for perfect randomness.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by grumpy View Post
    Hodor: a die with two sides might be better known as a coin, and will only roll if it lands on neither side.
    Specifically, it will roll to the air conditioning vent.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by grumpy View Post
    There is then the problem that, depending on how srand()/rand() themselves are implemented, that rand()%6 may not give results uniformly distributed between 0 and 5. For example, they may be biased more toward some of the values.

    If you want particular distribution or other properties of your random values, you need to find a generator which supports those properties.
    The approved way to do it is

    // uniform random number on 0 -1 - epsilon
    #define uniform (rand()/(RAND_MAX+1.0))

    // uniform random number on 0 - N-1.
    #define rnd(N) ((int) ( (N) * uniform()))

    // typical six sided dice
    #define diceroll (rnd(6) + 1)

    That way you use all the bits of the random number, so you'll get better results. If you're serious about random number generation, you'll have to use something more sophisticated than rand() however.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Malcolm McLean View Post
    The approved way to do it is
    Approved by who?

    Your "approved" solution makes specific assumptions about the properties of floating point types and operations, for which there is no guarantee.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice Roll
    By seanksg in forum C Programming
    Replies: 4
    Last Post: 05-13-2011, 12:37 AM
  2. Dice Roll Program
    By HeidiPagel in forum C Programming
    Replies: 7
    Last Post: 12-13-2010, 10:39 AM
  3. Extreme beginner quadratic roots solver
    By browser in forum C Programming
    Replies: 3
    Last Post: 11-08-2009, 11:33 AM
  4. C++ dice roll need help
    By catdieselpow in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2007, 01:32 PM
  5. Total beginner, need extreme help
    By OUTOFANSER in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2006, 11:56 PM

Tags for this Thread