# Thread: Random Number Generator

1. ## 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.

2. Do something like
rand() % 100

3. Thanks man! That helped me reduce the integers to 1-100. All I need now is how to calculate and print the average.

4. 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:
Code:
```value = rand();
total += value;
count++;
// 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.

Popular pages Recent additions