Randomness of TF1::GetRandom() Function

I am simulating cosmic ray muons, and I am randomly assigning their directions and energies based on fits to cosmic ray muon data. I have stored these fits as TF1’s, but I end up generating the exact same muons every time I run the program (IE when I run 50 jobs in parallel). I can explain this with the following terminal input:

root -l
root [0] TF1* f1 = new TF1(“f1”,“exp(x)”,1,5)
root [1] f1->GetRandom()
(Double_t)4.99974641411417942e+00
root [2] f1->GetRandom()
(Double_t)3.27538489028452329e+00
root [3] f1->GetRandom()
(Double_t)3.78178317517900409e+00
root [4] .q

root -l
root [0] TF1* f1 = new TF1(“f1”,“exp(x)”,1,5)
root [1] f1->GetRandom()
(Double_t)4.99974641411417942e+00
root [2] f1->GetRandom()
(Double_t)3.27538489028452329e+00
root [3] f1->GetRandom()
(Double_t)3.78178317517900409e+00
root [4] .q

The same 3 numbers are drawn… What I need is for these three to be different each time I run my program and draw from the same functions. With other random number generators, I’ve solved this by seeding a system time (needs to be in nanoseconds/milliseconds, seconds aren’t fast enough for when I start multiple jobs in parallel at the same second). What is the best way to go about solving this problem? Is there a way to seed the GetRandom() function within TH1?

Thanks!

Initialize the random number generator with an argument=0 (see doc). In this case you will start with a different sequence every time you run your program. This works with parallel jobs too. Setting the argument to 0 guarantees that teh sequence is unique in time and space (processid, clock(nanoseconds))

TRandom3 r(0); TF1* f1 = new TF1("f1","exp(x)",1,5); f1->GetRandom(); //will use the current generator (here r or gRandom)
Rene

Good afternoon,
I am having the same problem of the one posted here.
I tried with your suggestion, but again, each time I launch the program the same numbers are generated.
Here is part of my code:

{
TRandom3 r(0);
//Function for bkg
double m_bkg = 3.33, s_bkg = 2.45, min_bkg = 0., max_bkg = 14.;
TF1 f_bkg = new TF1(“f_bkg”,"exp(-1.(x-[0])(x-[0])/(2.0[1]*[1]))", min_bkg, max_bkg);
f_bkg->FixParameter(0, m_bkg);
f_bkg->FixParameter(1, s_bkg);
double bkg = f_bkg->GetRandom();
cout << bkg << endl;
}

Thank you for your help,

Best Regards,
Ilenia

Instead of “TRandom3 r(0);”, write:
gRandom->SetSeed(0);

Thank you very much, now it works!

Ilenia