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:
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?
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:
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;
}
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;
}