Thread: random number generator help

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    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(
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.

    [edit]Spent to much time forming my post, tabstop beat me :P[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM