Thread: Random Numbers (mean)

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

    Random Numbers (mean)

    Hi, this is my first thread on this board. (sure many more will follow)

    Basically i have to create a random number generator that generates 1000 random numbers from 0-9 and then display the mean average of these numbers.

    I have been told by my lecturer that it should be about 4.5 however i am returned with a value of 4.0

    here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    main()
    {
    int randnum = 0 ;
    float mean;
    int sum = 0;
    int x;
    
     srand(time(0));
     
    
     for (x = 0; x <= 1000; x++)
       {
         
         randnum =((int)10.0*rand()/(RAND_MAX + 1.0));
         sum += randnum;
         
       }
    
     mean = sum / 1000;
    
     printf(" mean: %f\n", mean); 
      
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    When you perform mathematical operations in a prrogramming language, the types of the constants or variables are important. Dividing two ints produces an int result, whereas dividing two floats gives a float result.
    If the types differ then promotion rules apply that turn one of them into the same type as the other. In this case if we can change 1000 into 1000.0f (making it a float) and then we are attempting to divide an int by a float.
    What the promption rules then do is that the int is also promoted to a float and it performs float/float division, which will give around 4.5f, instead of int/int which rounds down giving you an int of 4.
    Note that this all happens before the result is assigned to the variable on the left hand side of the assignment statement.
    Last edited by iMalc; 10-08-2010 at 01:02 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    float a;
    a = 450/1000;
    The answer for a is NOT 4.5; because C uses integer division when no floating point is involved on the right side of the equation. Note: The rule is more complex then this; but you should already know it; you just forgot it.

    I think you also have a fence post error in your for loop.

    And, main should return an int; and be defined as such.

    iMalc, must write faster than me.

    Tim S.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    thanks

    yes, that is quite obvious!
    cant believe i didn't notice that

    Also, why should main return an int

    and the fence post error was me fiddling about in desperation

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >Also, why should main return an int
    Read this

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stahta01 View Post
    Code:
    float a;
    a = 450/1000;
    The answer for a is NOT 4.5; because
    You're right... The answer is 0.45 which an integer result will make into a 0 for you.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Ok you guys have been very helpful!

    As im sure you can tell i am new to C and also not that experienced in any other language.

    I have written another programme that is supposed to count up the frequency of each random number generated!

    I am fairly sure there will be a stupid mistake in this code due to the compile errors i recieve from gcc.

    Compile errors:

    Code:
    random2.c: In function ‘arrayswitch’:
    random2.c:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    random2.c:37: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    random2.c:5: error: old-style parameter declarations in prototyped function definition
    random2.c:5: error: parameter name omitted
    random2.c:53: error: expected ‘{’ at end of input
    Code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void arrayswitch(int)
    int frequency[9];
    
    int main()
    {
      int randnum ;
      float mean;
      int x;
    
      srand(time(0));
    
      for (x = 1; x <= 10,000; x++)
        {
         
          randnum =( 10.0*rand()/(RAND_MAX + 1.0));
          arrayswitch(randnum);
        }
      printf("Frequency of 0: %d\n", frequency[0]);
      printf("Frequency of 1: %d\n", frequency[1]);
      printf("Frequency of 2: %d\n", frequency[2]);
      printf("Frequency of 3: %d\n", frequency[3]);
      printf("Frequency of 4: %d\n", frequency[4]);
      printf("Frequency of 5: %d\n", frequency[5]);
      printf("Frequency of 6: %d\n", frequency[6]);
      printf("Frequency of 7: %d\n", frequency[7]);
      printf("Frequency of 8: %d\n", frequency[8]);
      printf("Frequency of 9: %d\n", frequency[9]);
      return 0;
      
    }
    
    void arrayswitch(int number)
    {
       switch(number)
        {
          Case '0' :frequency[0] = frequency[0] + 1; break;
          Case '1' :frequency[1] = frequency[1] + 1; break;
          Case '2' :frequency[2] = frequency[2] + 1; break;
          Case '3' :frequency[3] = frequency[3] + 1; break;
          Case '4' :frequency[4] = frequency[4] + 1; break;
          Case '5' :frequency[5] = frequency[5] + 1; break;
          Case '6' :frequency[6] = frequency[6] + 1; break;
          Case '7' :frequency[7] = frequency[7] + 1; break;
          Case '8' :frequency[8] = frequency[8] + 1; break;
          Case '9' :frequency[9] = frequency[9] + 1; break;
          default  :printf("ERROR"); break;
    
        }
    }

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    when that works i will tidy it up to make the printf statements a loop

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Btw, did you really want '0' '1' in your switch-case. or 0 , 1 ?
    Anway this big switch can be shortened.
    Your 'Case' should be 'case'.

    frequency can hold 9 int. 0-9 inclusive is 10 elements...


    Code:
      for (x = 1; x <= 10,000; x++)           // remove , from 10,000
    Edit: need a semicolon after your function prototype.
    Last edited by Bayint Naung; 10-09-2010 at 04:57 AM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Suggestions in red.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void arrayswitch(int);
    int frequency[9];
    
    int main()
    {
      int randnum ;
      float mean;
      int x;
    
      srand(time(0)); 
    
      for (x = 1; x <= 10,000; x++)
        {
         
         // randnum =( 10.0*rand()/(RAND_MAX + 1.0));
        randnum = rand() % 10;
    
          arrayswitch(randnum);
        }
      printf("Frequency of 0: %d\n", frequency[0]);
      printf("Frequency of 1: %d\n", frequency[1]);
      printf("Frequency of 2: %d\n", frequency[2]);
      printf("Frequency of 3: %d\n", frequency[3]);
      printf("Frequency of 4: %d\n", frequency[4]);
      printf("Frequency of 5: %d\n", frequency[5]);
      printf("Frequency of 6: %d\n", frequency[6]);
      printf("Frequency of 7: %d\n", frequency[7]);
      printf("Frequency of 8: %d\n", frequency[8]);
      printf("Frequency of 9: %d\n", frequency[9]);
      return 0;
      
    }
    
    void arrayswitch(int number)
    {
    
         ++frequency[number];
    
    /*
       switch(number)
        {
          Case '0' :frequency[0] = frequency[0] + 1; break;
          Case '1' :frequency[1] = frequency[1] + 1; break;
          Case '2' :frequency[2] = frequency[2] + 1; break;
          Case '3' :frequency[3] = frequency[3] + 1; break;
          Case '4' :frequency[4] = frequency[4] + 1; break;
          Case '5' :frequency[5] = frequency[5] + 1; break;
          Case '6' :frequency[6] = frequency[6] + 1; break;
          Case '7' :frequency[7] = frequency[7] + 1; break;
          Case '8' :frequency[8] = frequency[8] + 1; break;
          Case '9' :frequency[9] = frequency[9] + 1; break;
          default  :printf("ERROR"); break;
    
        }
    */
    }
    Last edited by CommonTater; 10-09-2010 at 05:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM