Thread: Generating rand() and max value

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    Generating rand() and max value

    The following program actually gives me two errors: The first is that the random generation works only SOMETIMES (no consistent value for aMax that will make it work correctly). The second problem is the MOLASSES speed of choosing the random numbers.

    The purpose of the program is to allow the user to input a "maximum sum" : the user enters 20. The program should choose random addends whose sums total NO MORE THAN 20 (the maximum sum the user entered). Is there an optimized way of doing this. One sort of came into my mind, but I never played with it, so therefore never figured out exactly how it would work. It would involve choosing b to between between a number and 30-a

    Anyways...any help is much appreciated. I'd like the program to be faster (problem #2), but my main concern is to make it work at ALL (#1).

    Thanks for the help!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void addTest(int);
    void subTest(int);
    void multTest(int);
    void setup(void);
    
    int prompt = 1;
    int aMax=, sMax, mMax;
    
    int main() {
        int choice;
        
        if ( prompt == 1) {
           prompt = 0;
           srand(time(NULL));
           setup();
           }
           
        else {
             system("cls");
             printf("\n\nMath Skills Menu:");
             printf("\n\t1. Addition (0-%d)" , aMax);
             printf("\n\t2. Subtraction (0-%d)" , sMax);
             printf("\n\t3. Multiplication (0-%d)" , mMax);
             printf("\n\t4. Set up ranges");
             printf("\n\t5. Exit");
             printf("\n\nEnter a selection: ");
             scanf("%d" , &choice);
        
             switch (choice) {
                   case 1:
                        addTest(aMax);
                        main();
                        break;
                   case 2:
                        //subTest(sMax);
                        break;
                   case 3:
                        //multTest(mMax);
                        break;
                   case 4:
                        setup();
                        break;
                   case 5:
                        printf("\n\nGoodbye!\n\n");
                        system("pause");
                        break;
                   default:
                           printf("\n\nInvalid selection. Try again!");
                           system("pause");
                           return 0;
                           main();
             } //end switch
        } //end else
    } //end main()
    
    void addTest(int aMax) {
         int a[20], b[20], resp;
         int right, wrong=0, percent; 
    
         int i;
         system("cls");
         printf("\nEnter -1 at any time to return to the menu.\n\n");
    
         for ( i=0; i<20; i++ ) {
             a[i] = (rand() % aMax) + 1;
             do {
                 b[i] = (rand() % aMax) + 1;
                 } while ( (a[i] + b[i]) > aMax );
         }
    
         for ( i=0; i<20 && resp!=(-1); i++) {
              
              start:
              printf("\n");
              printf("#%d. %d + %d = " , i+1, a[i], b[i]);
              scanf("%d" , &resp);
              
              if ( resp == -1 ) {
                 break;
                 }
              else if ( resp != (a[i]+b[i]) ) {
                   printf("  Try again!\n");
                   wrong++;
                   goto start;
              } //end else
         }//end for
         if ( resp != -1 ) {
         right = 20 - wrong;
         percent = 100 * right / 20;
         printf("\n\n %d of 20 questions correct: %d%%\n\n" , right, percent );
         system("pause");
         } //end if
    } //end addTest()             
         
    void setup(void) {
         system("cls");
         printf("\n\nSet up ranges:");
         printf("\n\nTest sums from 0 to ");
         scanf("%d" , &aMax);
         printf("\n\nTest differences 0 to ");
         scanf("%d" , &sMax);
         printf("\n\nTest multiples 0 to ");
         scanf("%d" , &mMax);
         main();
    } //end setup()

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How about letting a[i] only be 1 through aMax-1 (exclusive), thus guaranteeing that there's a least one value (aMax) that b[i] can be? So a[i] can be 1 through aMax-1. Then you just have b[i] be 1 through aMax-a[i].

    Code:
    a[i] = (rand() % (aMax - 1)) + 1;
    b[i] = (rand() % (aMax - a[i]) + 1;
    Or something like that. I haven't tested it, but you get the idea.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not sure I understand the problem. For the most part it seems like a roundabout way of asking for the divisors of a real number x and playing with them. If x has no divisors, then it would be the sum of an odd number and even number closest to x, or the square of the square root being the greatest solutions. It doesn't seem like it involves randomness at all.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's an example of what I mean. Even if I set aMax to 2 in my program it spits out "1 + 1 = 2" in no time at all. There's no "do overs" like in your program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
      int aMax = 10;
      int problems[20][2];
      int i;
    
      srand(time(0));
    
      for(i = 0;i < 20;++i)
      {
        problems[i][0] = (rand() % (aMax - 1)) + 1;
        problems[i][1] = (rand() % (aMax - problems[i][0])) + 1;
      }
    
      for(i = 0;i < 20;++i)
        printf("%d + %d = %d\n", problems[i][0], problems[i][1], problems[i][0] + problems[i][1]);
    
      return 0;
    }
    My output:
    Code:
    4 + 3 = 7
    1 + 7 = 8
    8 + 2 = 10
    2 + 6 = 8
    5 + 1 = 6
    3 + 1 = 4
    5 + 3 = 8
    8 + 1 = 9
    1 + 2 = 3
    7 + 1 = 8
    6 + 1 = 7
    6 + 4 = 10
    1 + 4 = 5
    4 + 6 = 10
    4 + 3 = 7
    7 + 2 = 9
    6 + 3 = 9
    4 + 4 = 8
    3 + 1 = 4
    1 + 4 = 5
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed