time independent random

Is it possible to generate a different random number using two identical codes at the same time? And if it is possible any ideas would be very appreciating.

In more words:
rand() operator return same number for every run.
After using srand ( time(NULL) ), the rand() operator return different number for every successive run. But if two identical codes are executed at the same time rand() of each codes still return the same number at the given time. How can I generate a different numbers at the same time without altering the codes?
How can I generate a different numbers at the same time without altering the codes?


Change the system time on one of the machines?
Change the system time on one of the machines?


I can not do that. I use clusters and I can not access those machines to change the system time.
Well, without altering your code I don't think this is going to possible then.
Use any other seed source:
std::random_device from C++
QueryPerformanceCounter from Windows
/dev/random from Linux
RAND_egd() from openSSL
etc
Use a high resoulution timer. Each OS has it's own, so it'll be OS dependent.

On Windows you can use QueryPerformanceTimer, on Unix you can use gettimeofday.
Thank you very much!

On the Linux this is working fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

int main()
{
	double x;
	unsigned int seed;
	FILE *urandom;

	urandom = fopen ("/dev/urandom", "r");
	fread (&seed, sizeof (seed), 1, urandom);
	srand (seed); /* seed the pseudo-random number generator */

	x=rand()/double(RAND_MAX); 

	cout<<x<<endl;

	return 0;
}
Topic archived. No new replies allowed.