# random number generator help

• 04-16-2010
mayoussa89
random number generator help
I need help with the random number generator- my range is -250 to 250, and i think i got the right format on that, but when i run the program, i get some garbage number, can someone help explain to me why this is, or what i have done wrong in my program?

also, can someone give me an idea as how to find the max value of the positive random integers- i think i might need a loop to do that but im not quit sure how to go about that. any help is greatly appreciated, thanks in advance!!

Code:

```//************************************************************************* // This program prompts the user to enter 'N', the number of random numbers // to generate and then prints these numbers to a file and returns them to // the main program. It also prints out the with the average of all the // negative integers and the maxiumum of the positive random integers. //******************************************************************** #include <iostream> #include <fstream> #include <cstdlib> using namespace std; ifstream inData("randNumbers.txt", ios::in); ofstream outData("randNumbers.txt", ios::out); int nRandomNumbers(int N); //declare all prototypes int main () {         int N, average, max, neg_avg, pos_max;         cout<<"Enter the value of N: ";         cin>>N;         average= nRandomNumbers(neg_avg);         max= nRandomNumbers(pos_max);         cout<<"The average of all negative integers is: "<<average<<endl;                 cout<<"The maximum of all positive integers is: "<<max<<endl;                 return 0; } int nRandomNumbers(int N) {         int i, randm;         int neg_sum=0;         int avg_neg, max_pos;         for (i=0; i<N; i++)                 randm= (-250)+ rand()%451;                 outData<<randm;         cout<<"The "<< N <<" random integers are: "<<randm<<" , "<<endl;                 for (i=0; i<N; i++)         {                 neg_sum++;         }                 if (randm<0)                {                 avg_neg= neg_sum/N;                 return avg_neg;         }         //if (randm>0)         //{         //        for( }```
• 04-16-2010
tabstop
If the last random number was negative, you will return 1 (avg_neg is always 1, since neg_sum is always N, no matter what). If the last random number was nonnegative, then you fall off the end of the function (hence your warning "not all control paths return a value") and return l;uhowriefuh9p8h, which could be anything.
• 04-16-2010
hk_mp5kpdw
Boy... let's get started shall we:

#1:
Code:

```int main () {         int N, average, max, neg_avg, pos_max;         cout<<"Enter the value of N: ";         cin>>N;         average= nRandomNumbers(neg_avg);         max= nRandomNumbers(pos_max);```
You are trying to pass in an uninitialized value into the function. The function takes this unknown value and tries to do stuff with it. It could be a negative value, it could be a billion, it could be 42... you have no idea. I'm thinking you wanted to pass in N here and not the two variables you are currently using.

#2
Code:

```for (i=0; i<N; i++)     randm= (-250)+ rand()%451;```
First off, your code there will generate numbers between +200 and -250 not the +250 and -250 that you said you wanted. Secondly, you haven't called srand anywhere in your code although that could be intentional for the purpose of testing your results. Thirdly, all you are doing is calling the function N times (remember from point#1 above that you are simply passing in an unknown/uninitialized value here) and throwing away all the numbers generated except for the last one which will be saved into the variable randm.

#3
Code:

```int neg_sum=0; ... for (i=0; i<N; i++) {     neg_sum++; }```
All this will do is set neg_sum to N (if N is positive) otherwise neg_sum will stay 0.

#4.
Code:

```int nRandomNumbers(int N) {     ...     if (randm<0)            {         avg_neg= neg_sum/N;         return avg_neg;     } ... }```
That bit of code contains the only return statement in that function (at least the version you've posted). This should not compile without warnings/errors because if randm happens to be greater than or equal to 0 then there is no return statement executed.

Also, remember that neg_sum is always going to be either 0 or N so the return value is always going to be 0 or 1 (provided randm is negative).

If your purpose is to generate N random numbers and find the max/average of those N values then you'll need to move the code that tracks that information into the loop that is generating the random values. You aren't able to currently access anything outside that random number generating loop but the very last value so any attempt to figure out a greatest or average value is already lost at that point.

Spent to much time forming my post, tabstop beat me :P[/edit]