Changing random seed for h->FillRandom("tf1",nev)

I want to randomly fill TH1F histogram using existing TF1 function. I do:

#include “TRandom3.h”

TRandom3 *r = new TRandom3();
r->SetSeed(11323221);

r->SetSeed(1003221);
h1->FillRandom(“t_mich”,606300);

But when I change the seed number, the resulting histogram h1 always looks the same.
Obviously, I am not changing the random seed. What I am doing wrong?
(I tried root-5.24, root-5.27.04, no change).

                                                                Thanks in advance, Emil

You’d better ALWAYS say “r->SetSeed(0);” (for TRandom1 , 2 or 3). This will ensure that ALL integers of the internal seed array are ALWAYS different every time you do it, as written in the http://root.cern.ch/root/html/TRandom3.html#TRandom3:SetSeed description.

BTW. You didn’t give your “t_mich” function so I cannot try it (with a standard “gaus”, I get different contents in the histograms). There is something strange in the code you showed. You create “TRandom3 *r”, then you do “r->SetSeed(…);” but then I see no place in which you actually use it. Maybe you wanted to say:

#include “TRandom3.h”
// …
if(gRandom) delete gRandom;
gRandom = new TRandom3(0);
// …
gRandom->SetSeed(0);
h_first->FillRandom(“t_mich”,606300);
// …
gRandom->SetSeed(0);
h_second->FillRandom(“t_mich”,606300);
// …

Thanks Pepe - read the URL and it indeed says that seed should be set to zero.
I try it now and the only problem is that does not work for me ;8-(
I am still getting always the same histogram back. I attach the whole code (that is short
and self explanatory). The h900010 and h900011 histograms (“raw” and “smoothed”)
are always the same, regardless of r->SetSeed(0)

                                                                                Cheers, Emil

pgate.C (2.52 KB)

Opps, sorry - now I read your post more carefuly and use the syntax

gRandom = new TRandom3(0);
gRandom->SetSeed(0);

After that change I get the different histograms every time.

                                                                 Thanks again, Emil
1 Like

Note that the default random generator is already a “TRandom3”. Try:
gRandom->Print();
So, it is sufficient to say (just once):
gRandom->SetSeed(0);
If, at any moment, you want to replace it with another one (any TRandomX, where X = 1, 2, 3), do:
#include "TRandomX.h"
if(gRandom) delete gRandom;
gRandom = new TRandomX();
gRandom->SetSeed(0);
Moreover, if you say:
gRandom = new TRandomX(0); // this sets the “seed” to the magic “0”, too
then, afterwards, you do not really need to say:
gRandom->SetSeed(0);