Random number generator

This is a discussion on Random number generator within the C++ Programming forums, part of the General Programming Boards category; Hi, Is there a quicker way of doing this bit of code.want to roll 5x ten sided dice and add ...

  1. #1
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26

    Random number generator

    Hi,

    Is there a quicker way of doing this bit of code.want to roll 5x ten sided dice and add the results.The way i have done it looks nasty,
    Code:
    int playerHealth=( (rand ()%10)+1)+( (rand ()%10)+1)+( (rand ()%10)+1)+( (rand ()%10)+1)+( (rand ()%10)+1);
    Do i have name each roll as a varible and then add the results?tried
    Code:
    int playerHealth=( (rand ()%10)+1)*5
    but that only rolls once and multiplys it by 5.

    any ideas?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    For convenience, I would put the dice roll in a function
    Code:
    int roll( int sides, int num_dice )
    {
        int sum = 0;
        for( int i = 0; i < num_dice; ++i)
            sum += rand() &#37; sides + 1;
    
        return sum;
    } 
    
    /* ... */
    
    int playerHealth = roll( 10, 5 );
    (untested)
    Last edited by Bench82; 09-01-2007 at 07:13 AM. Reason: fixed typo

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    what about
    Code:
    int playerHealth=( rand ()&#37;46)+5;
    bit less random but same range.

    Kurt
    Last edited by ZuK; 09-01-2007 at 07:15 AM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by ZuK View Post
    what about
    Code:
    int playerHealth=( rand ()&#37;46)+5;
    bit less random but same range.

    Kurt
    Actually, maybe a little too random - extreme' values would be more common, and the 'middle-road' values would occur less frequendly.. The distribution should be uneven (in theory) so that rolls with multiple die are typically biased towards the middle-road values.
    (Ask anyone who's ever worked as a croupier how often '7' comes up when rolling 2*6-sided die )

  5. #5
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    Thanks will try the function as it could be used across the board,only just started reading up on functions and prototypes but will give it a go

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Bench82 View Post
    Actually, maybe a little too random - extreme' values would be more common, and the 'middle-road' values would occur less frequendly.. The distribution should be uneven (in theory) so that rolls with multiple die are typically biased towards the middle-road values.
    The sum of multiple dice rolls is equivalent (well, only roughly) to drawing values from a Gaussian distribution. So you can do it with a single call to rand(), but the expression is a bit more complex.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by brewbuck View Post
    The sum of multiple dice rolls is equivalent (well, only roughly) to drawing values from a Gaussian distribution. So you can do it with a single call to rand(), but the expression is a bit more complex.
    Yeah, one rand() call will give the wrong distribution - flat.

    Try something like the above roll function, but I'd optimise all those +1's:
    Code:
    int roll(int sides, int num_dice)
    {
        int sum = num_dice;
        for( int i = 0; i < num_dice; ++i)
            sum += rand() &#37; sides;
    
        return sum;
    } 
    
    /* ... */
    
    int playerHealth = roll( 10, 5 );
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 05:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21