Should I use a random engine seeded from std::random_device or use std::random_device every time

后端 未结 6 1790
梦如初夏
梦如初夏 2020-12-30 00:16

I have a class that contains two sources of randomness.

std::random_device rd;
std::mt19937 random_engine;

I seed the std::mt19937

6条回答
  •  清歌不尽
    2020-12-30 00:27

    If you are not using it for encryption it is fine and well to repeatedly use mt19937 which is seeded by random_engine.

    For the rest of this answer, I assume you are using the random numbers for encryption in your networking code. In short, mt19937 is not suitable for that use.

    http://en.wikipedia.org/wiki/Mersenne_twister#Disadvantages

    There is a potential risk that you will leak information (perhaps indirectly) over time so that an attacker could start to predict the random numbers. At least in theory, but this is what it's about. From Wikipedia

    ...since this figure is the size of the state vector from
    which future iterates are produced) allows one to predict all future iterates.

    A simple means of preventing random number generation information to leak to the user is to use one-way hash functions, but there's much more to it. You should use a random number generator designed for that purpose:

    http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator

    Various examples (with code) are found here http://xlinux.nist.gov/dads/HTML/pseudorandomNumberGen.html

提交回复
热议问题