# Random int generating method always generates same number

Page 1 of 1

## 8 Replies - 1287 Views - Last Post: 01 September 2011 - 09:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=245617&amp;s=1bf2791859f89b6d24e89625b07b31a8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jp612

Reputation: 0
• Posts: 51
• Joined: 16-August 11

# Random int generating method always generates same number

Posted 01 September 2011 - 01:13 AM

Hi,

I'm using rand() function in C++. I Have created the following code:

```#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

/*
*
*/
void proccessTZ(int t, int z) {
if (t > z) {
cout << "t > z" << endl;

} else if (t < z) {
cout << "t < z" << endl;

} else {
cout << "error" << endl;
}

}

int makeTZ() {
srand((unsigned)time(0));
int a = (rand() % 10) + 1;
cout << a << endl;
return a;

}

int main() {
int t = makeTZ();
int z = makeTZ();
proccessTZ(t,z);

return 0;

}

```

I wrote the makeTZ() method and call it twice in my main method but it always gives me a random number thats the same for t and z. E.g t = 4 and z = 4 or t = 6 and z = 6 or t = 10 and z = 10. Why is it doing this?

Is This A Good Question/Topic? 0

## Replies To: Random int generating method always generates same number

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,997
• Joined: 30-May 10

## Re: Random int generating method always generates same number

Posted 01 September 2011 - 01:25 AM

> 24 srand((unsigned)time(0));
Do this exactly ONCE at the start of main(), not every time in a function.

For a short-lived program such as this, time() is effectively a constant, so you're always setting the same seed, and always getting the same rand()

### #3 Ecstatic

Reputation: 2
• Posts: 42
• Joined: 20-March 11

## Re: Random int generating method always generates same number

Posted 01 September 2011 - 01:31 AM

This is because rand() does not itself 'generates' a random number. It displays the second of your computer clock. This program runs in an instant of a second and the values to your t and z are assigned in the same second therefore it is not different. Hope you got what I said.

What you can do: Add some integer manually to your z so that when t=4, z=4+some integer you add

### #4 hulla

• Writing Lines

Reputation: 49
• Posts: 733
• Joined: 05-March 11

## Re: Random int generating method always generates same number

Posted 01 September 2011 - 01:32 AM

This is because the random value will change only every 1 second since you seeded it to time(0). The computer is so fast it is seemingly impossible to have t and z a different value.

You have to make your code wait after assigning t and then assign z if you want a realistic chance of t and z being different . . .

Edit of your code with a waiting function . . .

```#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

/*
*
*/
void proccessTZ(int t, int z) {
if (t > z) {
cout << "t > z" << endl;

} else if (t < z) {
cout << "t < z" << endl;

} else {
cout << "error" << endl;
}

}

int makeTZ() {
srand((unsigned)time(0));
int a = (rand() % 10) + 1;
cout << a << endl;
return a;

}

int main() {
int t = makeTZ();
Sleep(1207); // Sleeps for 1207 milliseconds
int z = makeTZ();
proccessTZ(t,z);

return 0;

}

```

This code waits for 1207 milliseconds before assigning z.

Hope this helps.

Edit: Salem_c is right. Just seed it once.

This post has been edited by hulla: 01 September 2011 - 01:34 AM

### #5 Karel-Lodewijk

Reputation: 454
• Posts: 864
• Joined: 17-March 11

## Re: Random int generating method always generates same number

Posted 01 September 2011 - 03:10 AM

Ecstatic, on 01 September 2011 - 01:31 AM, said:

This is because rand() does not itself 'generates' a random number. It displays the second of your computer clock.

This is wrong, rand does generate a 'pseudo' random number, which is as random as computers can get.

Salem_c has given you the right solution.

### #6 jp612

Reputation: 0
• Posts: 51
• Joined: 16-August 11

## Re: Random int generating method always generates same number

Posted 01 September 2011 - 03:19 AM

Salem_c, on 01 September 2011 - 01:25 AM, said:

> 24 srand((unsigned)time(0));
Do this exactly ONCE at the start of main(), not every time in a function.

For a short-lived program such as this, time() is effectively a constant, so you're always setting the same seed, and always getting the same rand()

Ah ok i get that. It works thanks.

### #7 Ecstatic

Reputation: 2
• Posts: 42
• Joined: 20-March 11

## Re: Random int generating method always generates same number

Posted 01 September 2011 - 09:06 AM

Karel-Lodewijk, on 01 September 2011 - 03:10 AM, said:

Ecstatic, on 01 September 2011 - 01:31 AM, said:

This is because rand() does not itself 'generates' a random number. It displays the second of your computer clock.

This is wrong, rand does generate a 'pseudo' random number, which is as random as computers can get.

Salem_c has given you the right solution.

I never knew it was like that... thanks

### #8 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Random int generating method always generates same number

Posted 01 September 2011 - 01:03 PM

rand() uses a mathematical forumula to generate some permutation of the numbers between 0 and RAND_MAX. Generally that formula is a LCG (liner congruential generator) however the standard does not dictate what algorithm to use (note in C++11 there is a new header random which lets you pic the algorithm used).

srand() is used to "Seed" the random number generator. we use srand(time(0)) to seed the random number generator with the current time because this is pretty hard to "guess" what the seed will be each time the program runs - therefore each time the program runs it will have a different set of "random numbers".

however if you use the same seed you get the same set of random numbers. So if you you update srand() twice in the same second with time(0) then you get the same "random" numbers from rand.

Sometimes getting the same set of numbers is desirable. For example in a card game using rand to shuffle the cards, if you srand() to the same number to you can "replay" the game. Other programs will save the number in a log file so that if there is some error the programmers can debug with the same set of random numbers that were used to produce the error. etc.

### #9 hulla

• Writing Lines

Reputation: 49
• Posts: 733
• Joined: 05-March 11

## Re: Random int generating method always generates same number

Posted 01 September 2011 - 09:23 PM

Just a note:
Computers cannot create true randomness.
They can only imitate it.
There will always be a pattern in the computer generated numbers because there is no formula to create true randomness without a changing-variable.
No changing variable the computer can access is truly random.
The best changing-variable is the computer time which always simply increments.

The pattern of "random" numbers may be hard to find and may take hours to find but it is still there.