Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random numbers in multiple processes at the same time?

Tags:

c

random

How can I generate random numbers in C for multiple processes being run at exactly the same time?

I wanted to use srand and rand but I don't know how (maybe using the process ID?).

like image 348
Shark Avatar asked Dec 01 '25 06:12

Shark


1 Answers

You can use a different seed for each process, based on the process id for example :

srand(getpid());

And then just use rand().

like image 85
Julien Fouilhé Avatar answered Dec 03 '25 19:12

Julien Fouilhé



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!