Home c++ STL C++ STL Random

STL C++ STL Random

Author

Date

Category

Lambda is needed by the generating number of the STL generator, normal dispersion. Minimum -50 Max 50. Compiler highlights Gen red and requires bracket, I do not understand why and where

auto gen = [] () {
  Return Std :: MT19937 Gen {std :: random_device () ()},
  STD :: uniform_int_distribution & lt; int & gt; UID (-50, 50);
};

Answer 1, Authority 100%

Full corrected code with a call example. The generator and distribution made static to do not create them with each call and re-use the created once. Instead of static, you can take them into a comprehensive function.

Try online!

# include & lt; random & gt;
#Include & lt; iostream & gt;
INT MAIN () {
  Auto Gen = [] () {
    Static std :: MT19937 RNG {STD :: RANDOM_DEVICE () ()};
    Static std :: uniform_int_distribution & lt; int & gt; Distr (-50, 50);
    RETURN DISTR (RNG);
  };
  For (Size_t i = 0; I & LT; 10; ++ i)
    STD :: COUT & LT; & LT; gen () & lt; & lt; "";
}

Conclusion:

37 10 -49 36 32 20 -32 -40 5 -37

The second version with the removal into the voltage function is suitable if this external function is rarely called compared to the lambda function. This option is distinguished by the fact that Thread-Safe i.e. safe with a lot of streaming, because Static variables in the first variant with a lot of streaming will compete.

Try online!

# include & lt; random & gt;
#Include & lt; iostream & gt;
INT MAIN () {
  STD :: MT19937 RNG {STD :: RANDOM_DEVICE () ()};
  STD :: uniform_int_distribution & lt; int & gt; Distr (-50, 50);
  Auto Gen = [& amp;] () {
    RETURN DISTR (RNG);
  };
  For (Size_t i = 0; I & LT; 10; ++ i)
    STD :: COUT & LT; & LT; gen () & lt; & lt; "";
}

Another option (modification of the first STATIC version), solving the problem of multithreading by adding mutexes. This option is slower, but can be used in multithreading, in a single-bill medium there is enough first option (first and faster).

Try online!

# include & lt; random & gt;
#Include & lt; iostream & gt;
#Include & lt; mutex & gt;
INT MAIN () {
  Auto Gen = [] () {
    Static std :: MT19937 RNG {STD :: RANDOM_DEVICE () ()};
    Static std :: uniform_int_distribution & lt; int & gt; Distr (-50, 50);
    Static std :: mutex mux;
    STD :: UNIQUE_LOCK & LT; STD :: MUTEX & GT; lock (MUX);
    RETURN DISTR (RNG);
  };
  For (Size_t i = 0; I & LT; 10; ++ i)
    STD :: COUT & LT; & LT; gen () & lt; & lt; "";
}

addition. As suggested @ Johnnikatsville, you can also use the thread_local , to solve the problem of multithreading. Those. The following code is safe in a multithreading environment and does not require the use of std :: mutex . In my opinion, this is the best of all 4 proposed code options.

Try online!

# include & lt; random & gt;
#Include & lt; iostream & gt;
INT MAIN () {
  Auto Gen = [] () {
    Thread_Local Std :: MT19937 RNG {STD :: RANDOM_DEVICE () ()};
    thread_local std :: uniform_int_distribution & lt; int & gt; Distr (-50, 50);
    RETURN DISTR (RNG);
  };
  For (Size_t i = 0; I & LT; 10; ++ i)
    STD :: COUT & LT; & LT; gen () & lt; & lt; "";
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions