1. ## 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. 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)

Code:
`int playerHealth=( rand ()&#37;46)+5;`
bit less random but same range.

Kurt

4. Originally Posted by ZuK
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. 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. Originally Posted by Bench82
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. Originally Posted by brewbuck
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 );```