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

hello, Ilenia.

I also had this problem for the same generated number and didn’t know how to solve it.
The following is the simplified code.
{
TRandom3 r(1);
TF1* f1 = new TF1(“f1”,“exp(x)”,1,5);
double ff=f1->GetRandom();
cout<<ff<<endl;
}

Thanks for your help.
Best Regards,
Jiechen

TRandom3 r(0);

Hi, Wile_E_Coyote,
I tried TRandom3 r(0) firstly, but it can not work.

Bests,
Jiechen

That’s because you do not use your “r”.
You need: gRandom->SetSeed(0);

Hi, Wile_E_Coyote,
Thanks very much, I tried gRandom->SetSeed(0), it works.
However, I had two question about it.
1.I didn’t define the object “gRandom”, but it can work in my main function.
2. The following code will be break, as you can see, if I invoke function “fun”. But using the command I noted directly, it can work. I’m very confused.
Double_t fun(Double_t *x,Double_t p)
{
double f=p[0]exp(x[0]);
return f;
}
void tt()
{
gRandom->SetSeed(0);
TF1
f1 = new TF1(“f1”,fun,1,10);
//TF1
f1 = new TF1(“f1”,“[0]*exp(x)”,1,10);
f1->SetParameter(0,1);
double ff=f1->GetRandom();
cout<<ff<<endl;
}

Bests,
Jiechen

TF1
TRandom

Wile_E_Coyote, thanks very much, I found my mistake in my code.

Bests,
Jiechen