"srand" function and how to implement it.

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.
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
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
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.
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, .......){?
You only seed a random generator once (generally) in each program. So get the integer and seed it maybe...the first thing you do?
ciphermagi probably meant srand(x); instead of srand(time(x));
How do I paste code into this?
[ code ]// put code here[ /code ]
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;
	}
	
}
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
Oh! I got it! Thanks a lot! And special thanks to you ciphermagi!
lowerbound + rand() % upperbound;

should be

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

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