Which is more random: human- or software-generated number?

前端 未结 7 1332
温柔的废话
温柔的废话 2021-01-13 12:16

Does it toss a coin to get random bit?
Or throw a die to get a random integer from 1 to 6?
Or take a car

7条回答
  •  天涯浪人
    2021-01-13 13:16

    A compiler does absolutely nothing with regards to requiring a random number. A compiler merely makes your code, call some other code which returns a random number. Now the "other code" which calls the random number might be:

    1. Implemented within your code.
    2. Implemented as a part of a standard library.
    3. Implemented as a part of a special library.

    In cases (1) and (2), it is mostly a pseudo-random algorithm, and the numbers you get aren't really random. If it is part of a standard library (like cmath or math.h), then one cannot say for sure that the result values are pseudo-random because the standards specify only the definition and not implementation.

    EDIT: The library is stdlib.h and it is ALWAYS a psuedo-random number as pointed out by Joey and phresnel. Read comments for details on their answers. I apologize for the error and I agree that I should have known better than to reply on instinct.

    Special libraries may be used which may have special implementation of other algorithms, like the Mersenne Twister algorithm. Also, they may be nothing more than drivers of Hardware that can generate random numbers. Hardware random number generators return somewhat "true" random numbers http://en.wikipedia.org/wiki/Hardware_random_number_generator.

    Random number algorithms from standard libraries eventually map to system calls on the OS. So, on linux, for example, you may simply be reading from /dev/random or /dev/urandom (or you may be doing the same thing in your own code as well).

    Also note that, true randomness can be achieved without using dedicated hardware or some dedicated service. /dev/random and /dev/urandom provide random numbers, which for all intents and purposes can be considered true.

    EDIT: Some special libraries or your own code may even be using a network service for random numbers (many of which provide true random numbers).

提交回复
热议问题