Thread: Random Number Issues

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

    Question Random Number Issues

    I am very new to C and have been tasked to produce a program that generates 1000 random numbers (with a max of 9) and then finds the total average. I have produced the following but it only seems to return 0 as the answer.

    Code:
    // Program to determine the mean of 1000 random numbers //
    // written by Kerim Morris 01/10/2010 //
    
    
    #include <stdlib.h>
    
    int max_count = 1000;         //declare the variables//
    int count = 0;
    int random_number = 0;
    int random = 0;
    int random_total = 0;
    float mean = 0;
    
    
    main()                       //start of main function//
    {
    {
                                 
    for (count = 0; count < max_count; count ++);   //loop counter to a maximum// 
                                                                       //of 1000 //
    
    random_number = (int)(10.0*rand()/(RAND_MAX+1.0));      //produce random//
                                                                              // numbers between 0-9//
    
    
    random_total = random_number + random_total;        //calculate the running //
                                                                              // total//
    }
                mean = (random_total / max_count);           //calculate mean of total //
                printf("The mean of 1000 random numbers equals %f");
               
                
    }
    Any guidance would be appreciated.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    for (count = 0; count < max_count; count ++);   //loop counter to a maximum//

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    I'm not quite sure what you mean, you seem to have repeated the same code I have but unfortunately there is no explanation.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yeah I think his blue semi-colon where there shouldn't be a semi-colon isn't that visible.
    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"

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Code:
    for (count = 0; count < max_count; count ++);   //loop counter to a maximum//
    There, that's a bit more obvious I think. At least when you've figured out that upside down, slightly off, italic exclemation point is in fact a semicolon.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    1) The semicolon isn't supposed to be there is their way of saying...
    Put opening brace instead.
    2) Also you have en extra opening-brace after main (one too many).
    3) This is incorrect:
    Code:
    mean = (random_total / max_count);
    . You need to do
    Code:
    mean = ((float)random_total / max_count);
    4) You need to fix your printf so that ot outputs mean.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  2. Random Number Issues
    By sansterre in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2009, 05:59 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM