Better seeds than time(0)?

前端 未结 11 1876
挽巷
挽巷 2021-01-02 08:06

I understand that time(0) is commonly using for seeding random number generators and that it only becomes a problem when the program is being run more than once per second.

11条回答
  •  感情败类
    2021-01-02 08:49

    You will need an alternative/secondary source of entropy. Depending on how much entropy you want to use, you can calculate a hash of any of the following inputs and use it as a seed for your final generator:

    • declare an unintialized random size char array on the stack
    • allocate a random bytes of memory
    • ask the user to move the mouse
    • ask the user to put random CD in the CD drive and read random bytes at random location from the first track
    • open the user's microphone or camera, collect random number of seconds of input, calculate a hash and seed
    • Windows: use CryptGenRandom to get a buffer of cryptographically random bytes
    • Unix: as others mentioned, read from /dev/random

提交回复
热议问题