# Trying to make rand different per program run

• 01-17-2002
Dreamerv3
Trying to make rand different per program run
Hello everybody, first off I'd like to say thank you to everyone who helped me with my while loop dilema two weeks or so ago.

I am now in the process of expanding the functionality of my guessing program so that it will generate random numbers which are different each time the program is run, I would ideally like to be able to set a range and then compile, as it is now my numbers are constrained only by the limits of the unsigned short

I would like to be able to state an exact range of numbers for example "X-XXXXX" or "X-XX" or "X-XXX" and so on.

I am using bloodshed dev c++ 4 and have used the rand() function successfully to generate a random number however I have not been able to control the range of the number. Anther big problem is that the number doesn't change everytime the program is run, I would like the a unique random number to be generated everytime the program is run.

I have remarked by attempts in this venture thus far in the source code below.

Here is my source code:

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{

unsigned short correctguess;
unsigned short attempt;

//This is what i'v tried so far in order to get a DIFFERENT random number
//everytime the program runs...

//srand (correctguess);
//int rand(srand n+2);

//And the above has not worked.

rand (correctguess);
//rand is 18467 everytime the program runs.
correctguess = rand();

cout << "Pick a number between 0 and 65535:" << endl;

cin >> attempt;

if (attempt == correctguess) //First try win contingency
cout << "Thats right on the money!" << endl;

while (attempt != correctguess) //Begining of guessloop
{

while (attempt > correctguess) //Begining of high rejection
{
cout << "Try a little lower." << endl;
cin >> attempt;
} //End of high rejection

while (attempt < correctguess) //Begining of low rejection
{
cout << "Thats too low." << endl;
cin >> attempt;
} //End of low rejection

} //End of guessloop

cout << "Thats right on the money!" << endl; //Normal win message
return 0;
}

I would really appreciate any help I recieve and thank you very much!
• 01-17-2002
golfinguy4
To get a different random # everytime, use this code for random numbers

Code:

#include <time>
.....
srand(time(NULL));
number=rand();

• 01-17-2002
Dreamerv3
It worked

Thank you!

Now please tell me where I went wrong?

Pasting code, having it work and then leaving it alone just doesn't sit right with me unless I know what it is doing...

Any reference you can point me to?

Or just a brief decription of what time does?

Incidentally, #inlcude <time> didn't work in dev c++ 4 however #include <time.h> did work.

Do I need to use the time header when I want a number to be shuffled differently each time?
And how do I control the range?
• 01-17-2002
Stoned_Coder
search for random numbers on these boards and check a few threads. eventually you will come across a nice formula that will take care of all your random number questions.
• 01-17-2002
taylorguitarman
rand() is a pseudo-random number generator. It will create the same sequence of random numbers with a given seed. The one used in srand(). So you typically use time() as the seed to srand() so the sequence will be different each time the program runs.
There are advantages to having the same sequence though in programming. You can create large worlds without the large space required to hold all of the data.
• 01-17-2002
Dreamerv3
I see, so time() tells rand to use a different seed per program run or per each time the program runs...

Very good, I will search the board(s) right now and see what I can find...
• 01-18-2002