+ 2

# rand() vs <random>

I want a exact answer to this:- who will win between the duo of srand() and rand() and the lib <random> Also pls provide some explanation on how to USE the <random> I looked up cplusplus.com , but I neither understood the concept of generators nor understood the distributions :v I want to do:- 1. Shuffle arrays and vectors 2. Generate random numbers with min and max

7th Jun 2019, 11:31 AM
Shahil Ahmed
5 ответов
+ 3
The random library from C++11 is generalized to support different types of generators, and also provide distributors. A distribution helps normalize the resultant value from a generator to a preferred range. Also, a distribution can also modify the probability of certain values from the range. For example, in a uniform_int_distribution, every integer from the given range has an equal probability of being generated. But in special distributions like the binomial_distribution, the probability of a number i in the range 0 to n is determined by C(n,i)*(p^i)*((1-p)^n-i), where p is the chance of success given in initialization. This way you can prefer certain value over others, like have a game spawn more common enemies and less rare items. This is why random is better. For more information, you can refer to: https://www.google.com/url?sa=t&source=web&rct=j&url=https://isocpp.org/files/papers/n3551.pdf&ved=2ahUKEwi6gtqysdfiAhURU30KHWpPDssQFjAMegQIBBAB&usg=AOvVaw3jxcioUwHrrs5F8jdKWCBz
7th Jun 2019, 1:45 PM
Kinshuk Vasisht
+ 4
Not really, sorry ... I never used it
7th Jun 2019, 11:53 AM
Baptiste E. Prunier
+ 3
In fact, it depends on what you want to do srand is bad because the number of time you need to call rand to have the same result twice is not really big For cyber security or simulation, it is a big issue ! For a more or less game, you do not care
7th Jun 2019, 11:36 AM
Baptiste E. Prunier
+ 3
The rand function is a linear congruential generator (generator with a linear function) used to generate values from a given function. A generator is a function that produces a new number using a function based on an existing number called the seed. But successive program executions might use the same seed, so usually the processor time is used as a seed. Specifying the seed is the task of srand. Rand uses a seed value to generate different functions which generate different values. But rand has a small range, as its maximum is RAND_MAX, cannot handle floats and ranges very easily and all the values from the range don't have equal probabilities of being generated. The C++11 <random> library provides numerous generator functions wrapped in helper classes. For example, the random library provides the mersenne_twister_engine, which is faster as compared to a linear congruential generator like rand.
7th Jun 2019, 1:38 PM
Kinshuk Vasisht
+ 1
Hmm , then I think I should learn both , can you provide some good sources to learn <random>? I don't get the concept of generators and distribuitors
7th Jun 2019, 11:40 AM
Shahil Ahmed
Актуальное сегодня
HELP ME