"srand" function and how to implement it.

Feb 22, 2012 at 2:40am
I missed the whole week that this was covered. And I cannot find any help from the course book. All help is greatly appreciated.

Write a function named generateRandom that generates random numbers in a range specified by the user. The function takes two integer parameters representing the lower and upper bounds of the range and it returns an integer random number between the lower and upper bound inclusive. For example, if the parameters are 5 and 25, the function will return a random number between 5 and 25. Write a main function that tests this function by:
1) Asking the user for an integer with which to seed the randon number generator. Call srand with this integer.
2) Ask the user for a lower and upper bound for the random number.
3) Call generateRandom and print the generated random number.

I know that I have to include
#include <cstdlib>
#include <ctime>

It's just where to go from there. Thanks again in advance for all the help.
Feb 22, 2012 at 2:51am
srand (time ( 0 ) ); //sets the seed (based on the time)

1
2
3
4
int somefunction(lowerbound, upperbound){
somerandomnumber = lowerbound + rand() % upperbound;
return somerandomnumber;
}
Last edited on Feb 22, 2012 at 2:52am
Feb 22, 2012 at 6:08pm
I'm not exactly able to follow where you are going with this. I don't see where you are answering number 1. x_x
Feb 22, 2012 at 6:14pm
He's not.

1) Get an int from the user, say x. Use it to seed the generator. srand(time(x)); for example.
2) Get a lower bound from the user, and an upper bound.
3) Call generateRandom and just cout the random integer, like oonej showed you.
Feb 22, 2012 at 6:37pm
Hm.. Ok. That makes sense. As of right now, my question is where do I insert the "srand (time(x));"? Does it go after the headers? Or before the "int somefunction (lowerboud, .......){?
Feb 22, 2012 at 6:38pm
You only seed a random generator once (generally) in each program. So get the integer and seed it maybe...the first thing you do?
Feb 22, 2012 at 6:44pm
ciphermagi probably meant srand(x); instead of srand(time(x));
Feb 22, 2012 at 8:17pm
How do I paste code into this?
Feb 22, 2012 at 8:21pm
[ code ]// put code here[ /code ]
Feb 22, 2012 at 8:22pm
Never mind. I figured it out. This is what I have so far. Along with multiple errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
	int x, LowerBound, UpperBound, SomeRandomNumber; 
	cout << "Please insert an integer: ";
	cin >> x;
	srand (time (x));
	int somefunction(lowerbound, upperbound) {
		SomeRandomNumber = LowerBound + rand(x) % UpperBound;
		return SomeRandomNumber;
	}
	
}
Feb 22, 2012 at 8:34pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int somefunction(int lowerbound, int upperbound) // Functions are declared outside the main function. 
                                                  // Also, define the type of variable being passed.
{
    return lowerbound + rand() % upperbound;
}

int main ()
{
	int x, LowerBound, UpperBound, SomeRandomNumber; 
	cout << "Please insert an integer: ";
	cin >> x;
	srand (x); // Take the time() out of the random seed generator.
	// Somewhere down here, find out what the lower bound is, and the upper bound, and then 
             // call the function somefunction
}
Last edited on Feb 22, 2012 at 8:35pm
Feb 23, 2012 at 1:08am
Oh! I got it! Thanks a lot! And special thanks to you ciphermagi!
Feb 23, 2012 at 1:32am
lowerbound + rand() % upperbound;

should be

lowerbound + rand() % (upperbound + 1 - lowerbound);

(assuming upperbound is included, else remove the +1)
Topic archived. No new replies allowed.