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.
- Is there a disadvantage in the ceremony every time immediately and seeding again? I read, but it did not fully understand it.
- I used the
std :: uniform_real_distribution & lt; Double & gt;
As a square variable, but I have kept such errors which are notuniform_alion_wvid
member'sstd ::
why not? - I
std :: shared_ptr & lt; Std :: uniform_real_distribution & lt; Double & gt; & Gt; _iniform_distribution
as a class variable, and errors are still available. - What is the correct way to refer to a random generator?
- 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.
c ++ शुरुआती के लिए नमूना कोड
ReplyDeleteखोज एल्गोरिथ्म सीमा में पहले तत्व का पता लगाता है [पहला, अंतिम)