std::uniform_int_distribution isn't random enough

前端 未结 4 1192
面向向阳花
面向向阳花 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:48

    As others have mentioned, your main problem is that you're not seeding your engine. I thought I'd address something else since you note that speed and quality is important to you.

    Unlike other engines, default_random_engine makes no guarantees. Do not use it if you have any reasonable requirement. It's only there if you don't know what you're doing and the application of it doesn't really matter. From the standard:

    Remark: The choice of engine type named by this typedef is implementation-defined. [ Note: The implementation may select this type on the basis of performance, size, quality, or any combination of such factors, so as to provide at least acceptable engine behavior for relatively casual, inexpert, and/or lightweight use. Because different implementations may select different underlying engine types, code that uses this typedef need not generate identical sequences across implementations. — end note ]

    A better choice might be mt19937, which has high quality and is very fast.

提交回复
热议问题