This small part of my program will generate 2 random values in a loop that will loop infinite amount of times until the condition is met. I generate 2 values then add the two values to into totalNum, that totalNum will then be passed to a pointMatch variable. I need pointMatch to take in only the first total then keep that same exact number throughout the program. After that the next generation of numbers will be generated then are added to a total. I need to check if that total is equal to the previous pointMatch, if not generate new numbers again until the condition is met. I just cant seem to keep pointMatch the same, its always equal to total.
Asked
Active
Viewed 67 times
-2
-
2what is that `7 + 1 = 6`??? – Sourav Ghosh Sep 25 '15 at 21:17
-
1I don't think anyone understands this question. – Iharob Al Asimi Sep 25 '15 at 21:18
-
1why is this `while(totalRoll != pointMatch) {` a while and not an `if`? It appears that it can never repeat. – Chris Beck Sep 25 '15 at 21:18
-
just a simple output of my printf, 7 is the next random number generated plus 1(second random number generated) and 6 is the total. – moe hajer Sep 25 '15 at 21:20
-
@ChrisBeck I think its because I need the printf to print out infinite amount of times until the totalRoll is == pointMatch – moe hajer Sep 25 '15 at 21:21
-
1@moehajer You make it here `pointMatch = totalRoll` then you can't expect the loop to go again, do you expect it to? – Iharob Al Asimi Sep 25 '15 at 21:22
-
@moe hajer: Can you work on the explanation / motivation of this code? If we don't know what you are trying to do at a high level its very hard to intuit what the problem is. Saying this "This small part of my program will generate 2 random values in a loop that will loop infinite amount of times until the condition is met." Is telling me what code you wrote, which I can plainly see. But rather what we need to know is why you want those random values and what you are supposed to be outputting. What is the significance of the point match variable? – Chris Beck Sep 25 '15 at 21:22
-
1You want this : `randValue = 1 + rand() % 6; secondRandValue = pointMatch - randValue;` yet you want `secondRandValue` to be random, but it can't because you are fixing the sum so that `randValue + secondRandValue == pointMatch`, is this what you want? In a sense, `secondRandValue = pointMatch - randValue;` makes it random anyway. – Iharob Al Asimi Sep 25 '15 at 21:23
-
@ChrisBeck I will post the output that I need – moe hajer Sep 25 '15 at 21:24
-
It sounds like a quasi Cee-lo simulator, but the rules are slightly off. I agree, this question needs some real clarification. – Marshall Davis Sep 25 '15 at 21:25
-
1@moehajer: Take that `srand()` call out of this function. See: [srand() — why call it only once?](http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – Blastfurnace Sep 25 '15 at 21:27
-
@iharob when I run my program, I always end up with random values for randValue and secondRandValue, so the program will loop forever printing new values and adding them to a totalRoll. But totalRoll is always the same as pointMatch. PointMatch should only be the sum of the first totalRoll. Program ends when the new totalRoll is equal to pointMatch. – moe hajer Sep 25 '15 at 21:40
-
@moehajer Try my solution please, does it do what you want? – Iharob Al Asimi Sep 25 '15 at 21:43
-
@iharob Your solution doesnt seem to work. Ive changed my solution and its works. The only think that does not work is pointMatch, how would i make pointMatch the same number. – moe hajer Sep 25 '15 at 21:58
-
You need to be more clear about what you want. – Iharob Al Asimi Sep 25 '15 at 22:01
-
@iharob firstRoll =1 seconRoll = 2 totalRoll = 3 pointMatch = 3......next run will be firstRoll =4 secondRoll =7 totalRoll = 11 pointMatch = 3...later on if totalRoll = pointMatch...programs ends – moe hajer Sep 25 '15 at 22:05
-
Stop using global variables, I think they are the fundamental cause of your problem. – Iharob Al Asimi Sep 26 '15 at 00:03
1 Answers
2
The problem is you ware trying to achieve a goal which is very simple through a very complicated approach, if you want pointMatch
to remain the same, then don't generate the second random value through rand()
instead, just subtract the first one from pointMatch
.
int rollDice() {
/* WRONG: just call this once in the whole program */
srand( time( NULL ));
/* pick a random value */
randValue = 1 + rand() % 6;
secondRandValue = pointMatch - randValue;
return randValue + secondRandValue; //return random total
}
Also, it seems as though all the variables are globals and that is generally a bad sign.
This result is as random as the default random number generator can generate random numbers for both randValue
and secondRandValue
, if you have any good argument against this approach, please share it.

Iharob Al Asimi
- 52,653
- 6
- 59
- 97
-
1Or even better, just `return pointMatch;` if the printing is just for debugging purposes. – John Bollinger Sep 25 '15 at 21:27