Why Does the Same Dice Result Repeat in My C Program's While Loop?

  • Thread starter Thread starter chasely
  • Start date Start date
  • Tags Tags
    Loops
Click For Summary
SUMMARY

The forum discussion addresses a coding issue in a C program designed to simulate rolling a pair of six-sided dice. The problem arises when the program incorrectly repeats the same dice result in a while loop due to the placement of the random number generator. The solution involves moving the srand((int)time(NULL) + 1); line outside the loop to ensure a new seed is generated for each roll. The program also utilizes conditional statements to score points based on the results of the dice rolls.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with the rand() and srand() functions in C
  • Knowledge of conditional statements, specifically if-else constructs
  • Basic understanding of random number generation and its applications
NEXT STEPS
  • Learn about proper usage of the srand() function in C programming
  • Explore the concept of random number generation and its implications in simulations
  • Investigate best practices for naming conventions in programming variables
  • Study the use of loops in C, particularly while loops and their control structures
USEFUL FOR

C programmers, students learning about random number generation, and anyone interested in improving their understanding of loops and conditional logic in C.

chasely
Messages
2
Reaction score
0

Homework Statement



Write a program that will roll a pair of six-sided dice (int variables named redDie and blueDie) and will give you 5 points every time you roll doubles (pair of 2s, 3s, 4s, and 5s) and 10 points if you roll snake-eyes (pair of 1s) or boxcars (pair of 6s). Run your program 10 times and see how many points you make. NOTE: This lab presents an interesting coding problem and emphasizes that gambling is for people who can’t do the math!

Homework Equations



This was given to us for help at the beginning of the equation

1. Paste the code below this function above your main() function and use it to generate random numbers. The syntax to obtain a random number in main will be :

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

int main(void)
{
int redDie, blueDie;

srand((int)time(NULL) + 1);

redDie = (rand() % 6) + 1 /* produce a random number 1 to 6 */
blueDie = (rand() % 6) + 1 /* what is the % used for? */


}

2. You will need to use the conditional “if-else” statement (Ref: Page 158 of textbook)
Some thing like the following:

if ((redDie == 1) and (redDie == blueDie))
{
/* snake eyes */
}

And so on...

The Attempt at a Solution



Here's what I got. The program runs just fine without the when loop, but with the loop in place, it takes the output of the first roll and copies that output 10 times, instead of executing a new roll at the beginning of each loop.

Any help that you could give me would be really helpful.

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

int main ()

{

int redDie;
int blueDie;
int dierolls;

dierolls = 0;
while (dierolls < 10)
{
srand((int)time(NULL)+1);

redDie = (rand()%6) + 1, /*produce a random number 1 to 6*/
blueDie = (rand()%6) + 1; /*what is the %used for?*/

if(redDie == 1 && redDie == blueDie)
{
printf("You rolled snake eyes, and scored 10 points!\n");/*snake eyes*/
}
else if(redDie == 6 && redDie == blueDie)
{
printf("You rolled a boxcar, and scored 10 points!\n"); /*boxcars*/
}
else if(redDie == 2 && redDie == blueDie)
{
printf("You rolled double 2's, and scored 5 points!\n");
}
else if(redDie == 3 && redDie == blueDie)
{
printf("You rolled double 3's, and scored 5 points!\n");
}
else if(redDie == 4 && redDie == blueDie)
{
printf("You rolled double 4's, scored 5 points!\n");
}
else if(redDie == 5 && redDie == blueDie)
{
printf("You rolled double 5's, and scored 5 points!\n");
}
else if (redDie != blueDie)
{
printf("You didn't roll any doubles, no points for you. Sorry...\n");
}
dierolls = dierolls + 1;

}
printf("Do yourself a favor, play a different game.");
return 0;
}
 
Physics news on Phys.org
the %6 part means "remainder after division by 6". For instance, "9 % 5" is 4 since 4 is the remainder when you divide 9 by 5.

I have no more answers for you, but I do have a question of my own. Why is it that you have capitalized the second word in redDie and blueDie, but not in dierolls? Do you think it is a good use of your scarcest resource to spend it on deciding case by case what standard to apply when naming things?
 
jimmysnyder said:
the %6 part means "remainder after division by 6". For instance, "9 % 5" is 4 since 4 is the remainder when you divide 9 by 5.

I have no more answers for you, but I do have a question of my own. Why is it that you have capitalized the second word in redDie and blueDie, but not in dierolls? Do you think it is a good use of your scarcest resource to spend it on deciding case by case what standard to apply when naming things?

I have no idea why they are capitalized...they were the variables given by the department. Maybe it would make it better for grading purposes? Thanks for your answer to the first part!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K