std::uniform_int_distribution isn't random enough

前端 未结 4 1189
面向向阳花
面向向阳花 2021-01-07 15:16

I want to randomly select an integer among n numbers, where n is small (say 5). To do this, I am using std::uniform_int_distribution.

The exact details

4条回答
  •  灰色年华
    2021-01-07 15:45

    Try to seed the random engine. Time is a good choice. Read this to get familiar to another ways.

    std::default_random_engine generator( (unsigned int)time(0) );
    

    or you can use std::random_device which tries to produce non-deterministic random numbers

    std::default_random_engine generator( std::random_device{}() ); 
    

    std::random_device is a uniformly-distributed integer random number generator that produces non-deterministic random numbers.

    Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation.

    It uses a hardware random generator device or generates a pseudo random number. Useful to seed.

提交回复
热议问题