Sunday, 8 September 2013

C++ multiple random numbers adding up to equal a certain number

C++ multiple random numbers adding up to equal a certain number

Having a little trouble figuring this one out, I have a bunch of int
variables which are generating random numbers from 1-9. I need them to
generate the correct numbers to equal to a certain number, for example 30.
int numone = rand()%10;
int numtwo = rand()%10;
int numthree = rand()%10;
int numfour = rand()%10;
int finalsum;
do{
finalsum = numone + numtwo + numthree + numfour;
}while(finalsum !=30);
I am not sure if I am going about this the right way but when I try to
compile the above code, I don't even get a number and sometimes I get a
"Time limit exceeded" error, perhaps it is taking too long. Is there
another way I can do this?

No comments:

Post a Comment