forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice_roll.c
34 lines (30 loc) · 868 Bytes
/
dice_roll.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*******************************************************************************
*
* Program: Dice roll simulator
*
* Description: An example of how to simulate dice rolls.
*
* YouTube Lesson: https://www.youtube.com/watch?v=4FCxXG44SFM
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
// Seeds the random number generator
srand( time(NULL) );
// dice stores the number of dice rolls, roll will store each roll
int dice = 10;
int roll = 0;
// conduct each dice roll and output the result
for (int i = 1; i <= dice; i++)
{
// rand() % 6 produces a random number between 0-5, add 1 to shift to 1-6
roll = rand() % 6 + 1;
printf("Dice %d: %d\n", i, roll);
}
return 0;
}