Question about the rand() function

This is a discussion on Question about the rand() function within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a program that will prompt a user to enter a starting value and an ending value. ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    6

    Question about the rand() function

    I'm trying to make a program that will prompt a user to enter a starting value and an ending value. When that is done, I will take those values and call a function that will create a 100 random numbers for me within those two input values.

    Here is the code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;
    
    void main()
    {
    	srand (unsigned int(NULL));
    	int nRandomNumber = 0;
    	int nStartingValue, nEndingValue;
    	cout << "Enter a starting value: ";
    	cin >> nStartingValue;
    	cout << endl;
    	cout << "Enter an ending value: ";
    	cin >> nEndingValue;
    	cout << endl;
    	
    	for (int j = 0; j < 100; j++)
    	{
    		nRandomNumber = rand() % (nEndingValue + 1) + nStartingValue;
    		cout << nRandomNumber << " ";
    	}
    }
    I started the program in a main() function to see if the random numbers do come but they are always out of range. Any help would be great.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The mod part of the equation reduces the possible values to between 0 and one less than whatever you mod with. In your case, that means that you are making the possible values fall between 0 and nEndingValue. Then your code adds nStartingValue to it to make the range (0 + nStartingValue) to (nEndingValue + nStartingValue). Obviously, this is not what you want.

    In the case of actual numbers, if the starting value is 5 and the ending value is 20, then after the mod part you get numbers from 0-20, and after the addition you get 5-25. You want 5-20, so the problem is the mod part is not reducing the possible values enough. See if you can figure out how to change that part of the equation to get the right value.

    Or you can search for the proper equation, which is used a lot and should not be hard to find.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    thanks for your help

    sorry im a newb i dunno what mod means :P

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The &#37; part of the equation is the mod. It means find the remainder. So 20 % 3 is 2 and 10 % 5 is 0.

    A random number % 20 will always be somewhere between 0 and 19, which is why it is used in this equation to fit the possible values into a range.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    gotcha

    thanks again

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    Code:
    nRandomNumber = rand() &#37; ((nEndingValue - nStartingValue)+ 1) + nStartingValue;
    I got this. It works great now. Thanks

    P.S. I love this board.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21