How to simulate rolling two dice (game cubes) with values 1–6 in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Returns a random number from 1 to 6
int rollDice(void) {
    return (rand() % 6) + 1;
}

int main(void) {
    // Seed once
    srand((unsigned int)time(NULL));

    int dice1 = rollDice();
    int dice2 = rollDice();

    printf("Dice 1: %d\n", dice1);
    printf("Dice 2: %d\n", dice2);

    return 0;
}

 
 
/*
run:
 
Dice 1: 4
Dice 2: 2
 
*/

 



answered Feb 18 by avibootz
edited Feb 18 by avibootz

Related questions

...