8 Replies - 900 Views - Last Post: 01 September 2011 - 09:23 PM Rate Topic: -----

#1 jp612  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • 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  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,091
  • 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()
Was This Post Helpful? 2
  • +
  • -

#3 Ecstatic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? -3
  • +
  • -

#4 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Random int generating method always generates same number

Posted 01 September 2011 - 03:10 AM

View PostEcstatic, 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.
Was This Post Helpful? 0
  • +
  • -

#6 jp612  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-August 11

Re: Random int generating method always generates same number

Posted 01 September 2011 - 03:19 AM

View PostSalem_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. :smile2:
Was This Post Helpful? 0
  • +
  • -

#7 Ecstatic  Icon User is offline

  • New D.I.C Head

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

Re: Random int generating method always generates same number

Posted 01 September 2011 - 09:06 AM

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

View PostEcstatic, 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 :)
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1