Thread: Random Number Generator

  1. #1
    Registered User
    Join Date
    Oct 2019

    Random Number Generator

    Hey all, I'm trying to finish a homework assignment for my coding in C class. The program must do the following,
    1. Read in a random number of integers, one at a time
    2. The program should stop looping when it reads in a -1(do not include the -1 in your mean calculation)
    3. Calculates and prints the mean (average), remember if you enter 0 as a number, it is still a number and should be calculated in the mean.
    4. The mean should include fractional values

    Currently I have the code to print out 15 random numbers, but they are very high integers (i.e. 1276051653). I want to make the random number between [1-100] but do not know how to. Also, I am confused on how to calculate the average from a set of random generated numbers. I'm very new to programming, so thanks for any help! Also the loop is supposed to be a while loop.

    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Do something like
    rand() % 100
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Thanks man! That helped me reduce the integers to 1-100. All I need now is how to calculate and print the average.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    New York, NY
    You have to capture the return value from rand(), and keep count of how many values captured from rand(), in the while loop, then after the loop, divide the total by the count to get the average.

    For example:
    value = rand();  
    total += value;  
    // Then after the loop ends:  
    average = (float) total / count;
    Please use more descriptive names than a & b.

    If total and value are ints, you have to cast one of the values to a float to avoid integer math that only results in a whole number result.

    Please post your code in [ CODE ] blocks as specified in the Forum Rules FAQ above.
    Last edited by rstanley; 10-10-2019 at 09:03 AM. Reason: Format problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Generator Stuck on 1 Number?
    By Dollydaydream in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2013, 08:43 AM
  2. Random Number Generator Help
    By KrazySocoKid in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 08:57 PM
  3. Only getting 0; random number generator
    By scatterbrain in forum C Programming
    Replies: 10
    Last Post: 10-11-2011, 06:24 AM
  4. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  5. random number generator
    By begginer in forum C Programming
    Replies: 1
    Last Post: 02-23-2011, 08:52 AM

Tags for this Thread