Cannot open multiple instances of a program I wrote

Hello,
So, I've been trying to make one of my program open separate instances of another program I made. Let me show you what I'm trying to do.
Program A has a for loop that opens Program B ten times.
Program B prints out a RANDOM sequence of numbers and characters.
The problem I'm having is that after running Program A, the ten CMD windows of Program B have the same exact random values. I've been using CreateProcess(), ShellExecute() and system(), yet I'm still running into the same problem. Any help would be appreciated.
Last edited on
You either forgot to call srand or used time(0) as a seed. Since time(0) returns the number of seconds passed since Jan 01 1970, the value it returns only changes every second. So if you execute the program ten times within the same second, the numbers that are generated will be the same.
Use the low part of QueryPerformanceCounter or the RDTSC instruction instead.
Last edited on
Can you explain how to use the following to get different random numbers? I'm a college sophomore so I haven't gone far into c++ as of yet.
Okay, I looked up QueryPerformanceCounter and did the following:
1
2
3
4
5
6
7
8
9
LARGE_INTEGER temp, tc;
double val = 2;
QueryPerformanceCounter(&temp);
for(int i = 1; i < 1000000; ++i){
	val = pow(val, 2);
}
QueryPerformanceCounter(&tc);
tc.LowPart -= temp.LowPart;
srand((unsigned)tc.LowPart);

This allowed for different random numbers in each instance. If anyone has a more efficient way, please feel free to share.
I'm not sure what the loop is for and why you're using a counter difference (which is less random), but you should do it like this:

1
2
3
LARGE_INTEGER tc;
QueryPerformanceCounter(&tc);
srand(tc.LowPart);
Last edited on
There was a website with some example and it used the loop, but your method is much better. Thank you very much.
Topic archived. No new replies allowed.