C++ multiple random numbers adding up to equal a c

2019-03-06 19:00发布

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?

标签: c++ loops random
3条回答
老娘就宠你
2楼-- · 2019-03-06 19:32

The chance that you get '30' for a result is pretty small. You also don't get a random number from 1 to 9, you get them from 0 to 9.

Try this instead:

int numone,numtwo,numthree,numfour, finalsum;
do
{
   do
   {
     numone = 1+rand()%9;
     numtwo = 1+rand()%9;
   } while (numone+numtwo < 12);
   numthree = 1+rand()%9;
} while (numone+numtwo+numthree < 21);
numfour = 30-(numone+numtwo+numthree);
// for clarity
finalsum = numone + numtwo + numthree + numfour;

(Edit) After some lateral thinking: numthree needs to be between 30-1-(numone+numtwo) and 30-9-(numone+numtwo) -- perhaps there is room for a small further optimization there.

(Further edit) After the deliberations below I actually tested it, and indeed, this works per requirement:

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

int main (void)
{
  int numone,numtwo,numthree,numfour, finalsum;
  int i;

  srand(time(NULL));

  for (i=0; i<100; i++)
  {
    numone = 3+rand()%7;
    if (numone == 3)
      numtwo = 9;
    else
      numtwo = 12-numone+rand()%(7-(9-numone));
    if (numone + numtwo == 12)
      numthree = 9;
    else
      numthree = 21-(numone+numtwo)+rand()%(6-(18-(numone+numtwo)));
    numfour = 30 - (numone + numtwo + numthree);

    finalsum = numone + numtwo + numthree + numfour;
    printf ("%d + %d + %d + %d = %d\n", numone, numtwo, numthree, numfour, finalsum);
  }
}
查看更多
淡お忘
3楼-- · 2019-03-06 19:38

Well, i hope you realize that in your do while loop, you are not actually changing the values of the random numbers.What you are doing is basically checking the same condition of finalsum = numone + numtwo + numthree + numfour; infinitely if it does not equal 30.What you should do is:

int numone,numtwo,numthree,numfour,finalsum;
do{
numone=rand()%10;
numtwo=rand()%10;
numthree=rand()%10;
numfour=rand()%10;
finalsum = numone + numtwo + numthree + numfour;
}while(finalsum !=30);
查看更多
手持菜刀,她持情操
4楼-- · 2019-03-06 19:49

Move the rand() into the while loop, or you'll generate random numbers only once, and get stucked in while loop.

do{
int numone = rand()%10;
int numtwo = rand()%10;
int numthree = rand()%10;
int numfour = rand()%10;
int finalsum;
finalsum = numone + numtwo + numthree + numfour;
}while(finalsum !=30);
查看更多
登录 后发表回答