Wednesday 15 September 2010

c++ - How to reference random generator? -


I am trying to generate random within a range. I have multiple objects in multiple threads that allow this function to be Call, the function is in a singleton class, so only one object is called. But I noticed that my random number is close to the middle of the range and as a result, for the first 200 calls each other, they gradually spread to the given range.

  1. Is there a disadvantage in the ceremony every time immediately and seeding again? I read, but it did not fully understand it.
  2. I used the std :: uniform_real_distribution & lt; Double & gt; As a square variable, but I have kept such errors which are not uniform_alion_wvid member's std :: why not?
  3. I std :: shared_ptr & lt; Std :: uniform_real_distribution & lt; Double & gt; & Gt; _iniform_distribution as a class variable, and errors are still available.
  4. What is the correct way to refer to a random generator?
  5. What is the correct solution to this problem? / Li>

double getRandom (object * foo) {std :: random_device rnd; Std :: mt19937 gen (rnd ()); Std :: uniform_real_distribution & lt; Double & gt; Dis (0, foo- & gt; Borders); Double random = dice (gene); Random return; }

you have a random number generator only once seeds needed . In addition, your getRandom function is correct. How to use this as a way of using a class, how can you do this:

  # Include & lt; Random & gt; Class RNG {public: RNG :: RNG (): General (std :: random_device () ()) {} // Seeds MT 19 9 37 double getRandom (object * foo) {std :: uniform_real_distribution & lt; Double & gt; Dis (0, foo- & gt; Borders); Double random = dice (gene); Random return; } Private: std :: mt19937 gen; };  

At the beginning of the program, you instantiate a RNG , and mt19937 will be seeded once. Then you only call getRandom on the same code, whenever you need a random number.


1 comment: