Thread: Random Number Generator - Error

  1. #16
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by iMalc View Post
    If that were the case, how would you change the code to make it so that the y=y*x part was not inside the for-i loop any more?
    Stuck? That's because it is already is outside.

    Stick with the way you've written your program, it's fine, short of the small change with adding brackets. brack's code is wrong because it doesn't use the same random numbers for each power. He also either has no idea that you need real numbers in the range (0, 1], or doesn't understand how your code so simply does that. It's also more complicated than yours and far less efficient.

    BTW, you can shorten y = y * x to just y *= x
    You are right...my thoughts were different...my sorry

  2. #17
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    Ok, I cleaned up the code and took your advice. If I understood various nuances and pet peeves, I'd be a better programmer. I do welcome any structural comments. My averages though still yield zero. I'm trying to understand what am I missing here. Do you think the problem is with the printf function?

    Here is the edited code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (int argc, char *argv[])
    {
      unsigned int iseed = (unsigned int)time(NULL);
      srand (iseed);
      int i, j, average; //defines i and average as integers
      double x, y, ran, sum [15];
      for (i=0; i<15; i++)
         sum [i]=0;
      for (j=0; j<1000; j++)
    
      {
        //printf ("%u\n", rand ());
        x=y=(rand() / (RAND_MAX + 1.0));
        for (i=0; i<15; i++)    //error here - averages should be less than 1 (bracket)
        {
            sum[i]+=y;
            y*=x;}              //y=y*x
      }
    printf("The Sum of Random Numbers : %d ", sum);
    for (i=0; i<15; i++)
    {
        average=sum[i]/j; //j values retained from the loop (1000); alternatively: sum[15]
        printf("The Average of Random Numbers : %d i=%d\n",average, i);
    }
    
      return 0;
    }

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The average is not an integer, so why do you declare the variable as such, and then print it as an integer?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    Quote Originally Posted by laserlight View Post
    The average is not an integer, so why do you declare the variable as such, and then print it as an integer?
    See, this is why it takes me so much time to do this. I can't see simple stuff like that. I corrected that to double. But the code is still obviously wrong because I can't get a negative average and all the averages must be between 0 and 1.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (int argc, char *argv[])
    {
      unsigned int iseed = (unsigned int)time(NULL);
      srand (iseed);
      int i, j; //defines i and average as integers
      double x, y, ran, sum [15], average;
      for (i=0; i<15; i++)
         sum [i]=0;
      for (j=0; j<1000; j++)
    
      {
        //printf ("%u\n", rand ());
        x=y=(rand() / (RAND_MAX + 1.0));
        for (i=0; i<15; i++)    //error here - averages should be less than 1 (bracket)
        {
            sum[i]+=y;
            y*=x;               //y=y*x
        }
      }
    printf("The Sum of Random Numbers : %d ", sum);
    for (i=0; i<15; i++)
    {
        average=sum[i]/j; //j values retained from the loop (1000); alternatively: sum[15]
        printf("The Average of Random Numbers : %d i=%d\n",average, i);
    }
    
      return 0;
    }

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, based on what you described in post #8, I am inclined to suggest something radical: ditch the current structure of your code.

    Rather, create an array of 1000 doubles (let's name this numbers). Populate numbers with random numbers in the range [0.0, 1.0). Create another array of 1000 doubles to hold the powers of these random numbers (let's name this powers). Populate powers with 1s.

    Now, implement this algorithm:
    Code:
    for i in the range [1, 15]
        for j in the range [0, 1000)
            powers[j] = powers[j] * numbers[j]
        sum the powers
        compute the average
        print the average
    You could of course do this slightly differently, e.g., instead of populating powers with 1s, you populate it as a copy of numbers, and then modify the power computation algorithm to account for this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    agrrrhr I see so many duplications of effort!!!!

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    That sounds like a "strawman complaint" because you compared an example of "standard" (or rather, a common convention of) brace placement with no indentation versus an example of "non-standard" brace placement but with indentation. If we add indentation:
    Code:
    for (i= 0; i<15; i++){
        sum[i]+=y;
        y=y*x;
    }
    then your complaint about it being "not visually obvious where the loop begins and mismatched braces can be a frequent problem" is no longer valid, or at least as valid as a complaint against your personal style.
    Hi Lase...

    A lot of this is in what you get used to, of course. There may be some common convention, but I've never been big on "banging the same gong" in the first place The thing is, for some reason I just can't clearly see that leaging brace right after the bracket... so I put it where I can see it.

    Thanks for the response.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    There may be some common convention, but I've never been big on "banging the same gong" in the first place
    Have you worked with other programmers on a project, especially one where more than one programmer might work on the same source file? If so, then you can recognise the benefit of "banging the same gong", at least for each given source file. Code formatting tools can alleviate this problem, but they also complicate the development process where other tools (e.g., file differencing tools) are concerned.

    Quote Originally Posted by CommonTater
    The thing is, for some reason I just can't clearly see that leaging brace right after the bracket... so I put it where I can see it.
    You can use another common convention, e.g.,
    Code:
    for (i= 0; i<15; i++)
    {
        sum[i]+=y;
        y=y*x;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Have you worked with other programmers on a project, especially one where more than one programmer might work on the same source file?
    Not yet. So far this is just a big ole time waster for me (read: "hobby") so differences in style have never been an issue... yet.

    You can use another common convention, e.g.,
    Code:
    for (i= 0; i<15; i++)
    {
        sum[i]+=y;
        y=y*x;
    }
    I actually started out that way... but it seemed like a waste of space so I eventually shifted over to the way I do it now... I suppose if it ever became an issue I could adapt.

    Here's a sample of what my code looks like... As you can see I format the text very carefully and comment the heck out of everything. I'm hoping to come back to this in 5 years and still be able to interpret it...
    Code:
    // add program to list
    BYTE TrackerAddProgram(HWND Handle)
      { TCHAR     cl[MAX_PATH] = {0};   // command line
        BYTE    x;                      // counter
        // prevent seeing self
        if(Handle == MainWind)
          return 0;
        // find empty slot
        for (x = 1; x <= Setting.Programs; x++)
          { if (Programs[x] == NULL) // slot found
              { // get memory
                Programs[x] = malloc(sizeof(PGMINFO));
                memset(Programs[x],0,sizeof(PGMINFO));
                // get program handle
                Programs[x]->Program = Handle;
                // get the program name
                GetPgmName(Programs[x]->PgmName);
                // signal completion
                PulseEvent(LaunchEvent);
                AddToLog(L"Run",Programs[x]->PgmName);
                return x; } }
        AddToLog(L"Error",L"Too many programs");
        return 0; }

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