Thread: Random Number Generator Counts

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

    Random Number Generator Counts

    Code:
    // functon genRandom receives the four parameters to use for the pseud-random
    // number generator.  It generates the numbers, prints them, and calls 
    // the subfunction addToCounts for specific counts.
    void genRandom(int aSeed, int aLb, int aUb, int aNumToGen,
                   int* pEven, int* pOdd, int* pNeg, int* pPos, int* pZero )
    {
       // Local Declarations for srand
       int range = (aUb - aLb) + 1;
       int lineCount = 0;
       int randNum;
       srand(aSeed);
       // Local declaractions initialized for use in addToCounts
       *pEven = 0;
       *pOdd = 0;
       *pNeg = 0;
       *pPos = 0;
       *pZero = 0;
       
       // starts format for ouputs
       printf("\nNumbers generated:\n");
       
       // while loop used to obtain quanitity of randoms.  Per each iteration,
       // addToCounts is called to specify the type of each number generated.
       while (aNumToGen > 0){
          if (lineCount < 10)
             lineCount++;
          else{
             printf("\n");
             lineCount = 1;
             }
          printf("%4d", rand() % range + aLb);
          randNum = rand() % range + aLb;
          aNumToGen--;
          addToCounts(randNum, pEven, pOdd, pNeg, pPos, pZero);
          }
          
                     
    
    }
    // function addToCounts receives the pseudo-random numbers and counts the 
    // number of evens, odds, negatives, positives, and zeroes that occur.
    // It sends the counts back as output.
    void addToCounts(int randNum, int* pEven, int* pOdd,
                     int* pNeg, int* pPos, int* pZero)
    {
       
    // if statements update the counts
       if (randNum % 2 == 0)
          (*pEven)++;
       if (randNum % 2 != 0)
          (*pOdd)++;
       if (randNum < 0)
          (*pNeg)++;
       if (randNum > 0)
          (*pPos)++;
       if (randNum == 0)
          (*pZero)++;
    }
    
    // printResults will display all the users inputs,the pseudorandom numbers
    // generated and the counts all in aligned format
    void printResults(int aSeed,int aLb, int aUb, int aNumToGen,
                      int aEven, int aOdd, int aNeg, int aPos, int aZero)
    {
    
    printf("\n\n");
    printf("      Input Type                Value\n");
    printf("     -----------           ----------\n");
    printf("            Seed %20d \n", aSeed);
    printf("     Lower Bound %20d \n", aLb);
    printf("     Upper Bound %20d \n", aUb);
    printf("Number Generated %20d \n", aNumToGen);
    
    printf("\n\n");
    printf("      Count Type                Value\n");
    printf("    ------------                -----\n");
    printf("           Evens %20d \n", aEven);
    printf("            Odds %20d \n", aOdd);
    printf("       Negatives %20d \n", aNeg);
    printf("       Positives %20d \n", aPos);
    printf("           Zeros %20d \n", aZero);
    scanf("%d");
    }
    Last edited by Jaxtomtom89; 10-18-2010 at 11:02 AM.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    0 is even. Hence, whenever randNum is 0, both *pEven and *pZero will be incremented.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    // if statements update the counts
       if (randNum % 2 == 0)
          (*pEven)++;
       if (randNum % 2 != 0)
          (*pOdd)++;
       if (randNum < 0)
          (*pNeg)++;
       if (randNum > 0)
          (*pPos)++;
       if (randNum == 0)
          (*pZero)++;
    This will work but there's no exclusivity... it is possible that more than one counter can be incremented at a time... It's also possible to simplify the testing somewhat... For example if randbum %2 == 0 it can't be !0 so you could use an else statement...

    Code:
    if (RandNum % 2 == 0)
      *peven++;
    else
      *podd++;
    You can add some exclusivity (if desired) like this...

    Code:
    if (RandNum == 0)
      *pzero++;
    else
      { if (RandNum % 2 == 0)
               *peven++;
           else
               *podd++; }
    You can use the same approach to the < zero and > zero tests as well.
    One thing C does very well is complex conditionals...

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    To msh: Thank you for looking at it. I know it was a lot. The program is setup so that zero is counted as an even too. That's what our teacher wants. The problem is that counts are still off. Even when there is a zero in the numbers generated, it will show up as 0. All the counts just seem slightly off, but the conditions seem right to me at least.

    To common Tater: I will play around with the conditionals and if statements as you say. Thank you much.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    And we have our winner!
    Code:
    printf("%4d", rand() % range + aLb);
    randNum = rand() % range + aLb;

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    At msh: Thanks for looking at it again. I found it out as soon as I took a second look at the loop.

    Swapped placements of randNum expression and printf, and set printf to print randNum instead.

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Good job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  2. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  3. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM