Difference between random number generating in main function and subroutine


ROOT Version: 6.14.04
Platform: Ubuntu 18.04
Compiler: gcc 7.3


Dear experts

I have a puzzle about the result of calling subroutine and do in main function when generating random numbers. Here is the macro:

 void printrand()
 {
     TRandom3 r;
     std::cout << r.Uniform(0, 1) << std::endl;
 }
 void myrand()
 {
     TRandom3 r1;
     for(int i=0; i<5; i++) std::cout << r1.Uniform(0, 1) << std::endl;
     for(int i=0; i<5; ++i) printrand();
 }

Below is the output:

0.999742
0.16291
0.282618
0.947201
0.231657
0.999742
0.999742
0.999742
0.999742
0.999742

Why the result from calling subroutine turning out the same?
Many thanks in advance.

Yousen

#include "TRandom.h"
#include <iostream>
void printrand() {
  std::cout << gRandom->Uniform(0, 1) << std::endl;
}
void myrand() {
  gRandom->Print();
  gRandom->SetSeed(0); // make it really random
  for(int i=0; i<5; i++) std::cout << gRandom->Uniform(0, 1) << std::endl;
  for(int i=0; i<5; ++i) printrand();
}

Thanks, Wile. The macro works well. I have one more question. Do all TRandom3 variables initialize the same way so they have same values? The class reference says it is constructed by Seed=65539. And I look up TRandom3::SetSeed() . Is the real random number only produced by SetSeed(0)?

1 Like

The post is very helpful! Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.