-1

I have a C program that I run with a python script and the output is a text file containing random values (in a certain way). But when I run my program several times in a loop I got the same output text file each time (Or 2 different text files out of 100)

import subprocess
import time

start = time.time()

ntrial = input("How many trials? ")
for i in range(int(ntrial)):
    cmd = ["/Users/stordd/Desktop/StageI2M/C/forestenostre/grezza_foresta", "-w",
           "/Users/stordd/Desktop/StageI2M/Leiden/text_file/USA.txt", "-m", "5", "-e", "-0"]
    # Open/Create the output file
    outFile = open("/Users/stordd/Desktop/StageI2M/Leiden/k5/{}.txt".format(i), 'ab')

    result = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    out = result.stdout.read()

    outFile.write(out)
    outFile.close()

end = time.time()
print(end - start)
  • 2
    Hard to say without seeing the C program. Maybe you use the same seed each time? –  Jul 02 '19 at 12:36
  • I don't know C, someone gave me the program but each time I run the cmd line in the terminal I have different text files as output as predicted that's why i don't understand why it's not working here. – bruno stordeur Jul 02 '19 at 12:39
  • @brunostordeur can you post the C code? Otherwise it's nearly impossible to guess the reason. – Morten Jensen Jul 02 '19 at 12:41
  • 1
    @brunostordeur: Likely the random seed is based on the current time, which has a granularity of 1 second. If all the child processes are fired off within the same second, they’ll all have the same seed. Try adding a 1-second sleep in your loop and see if that makes a difference. – John Bode Jul 02 '19 at 12:42
  • Possible duplicate of [using rand to generate a random numbers](https://stackoverflow.com/questions/3159644/using-rand-to-generate-a-random-numbers). Note in particular [this](https://stackoverflow.com/a/3159655/4944425) answer. – Bob__ Jul 02 '19 at 12:43
  • It's really long and written in italian so I won't bother you with that. But yesterday It was working fine actually so was it just luck ? – bruno stordeur Jul 02 '19 at 12:44
  • @brunostordeur We´re mostly interested in seeing if/how the `srand()`-function is called in C :D I don't think the italian naming hinders readability too much. If you can upload the code or post it here, that would greatly improve our ability to help you :) – Morten Jensen Jul 02 '19 at 12:46
  • the only place I found the srand() was within this line ```srand48((long int) time(NULL));``` – bruno stordeur Jul 02 '19 at 12:52
  • The 1-second sleep in the loop worked ! I will be long for a lot of iterations but it's perfect thank you !! – bruno stordeur Jul 02 '19 at 12:55
  • Also note that you could add a little more entropy by adding as command line parameter the index used in the python program, read it in the C program and use it like `srand48((long int) time(NULL) + parameter * some_multiplier);` – Bob__ Jul 02 '19 at 13:01
  • 1
    @brunostordeur Congrats on getting the code to work :) You can replace `time(NULL)` with `clock()` or something similar, to get a better "resolution" than 1-second (and hence avoid or greatly reduce the sleep) – Morten Jensen Jul 02 '19 at 13:03
  • Another possibility (on a Unix-based system, anyway) is `srand(time(NULL) ^ getpid())`. – Steve Summit Jul 02 '19 at 14:06
  • I just replace ```srand48((long int) time(NULL))``` by ```srand(time(NULL) ^ getpid())```in the c program ? And then I can remove the time.sleep(1) in my python script ? – bruno stordeur Jul 02 '19 at 15:16

1 Answers1

1

You probably initialize your random generator with a seed based on the time in seconds. So, if you start the program with the same seed you will get the same random values.

tstenner
  • 10,080
  • 10
  • 57
  • 92