Is random.sample truly random?
<p>I have a list with <code>155k</code> files. When I <code>random.sample(list, 100)</code>, while the results are not the same from the previous sample, they look similar. </p>

<p>Is there a better alternative to <code>random.sample</code> that returns a new list of random 100 files? </p>

<pre><code>folders = get_all_folders('/data/gazette-txt-files')
# get all files from all folders
def get_all_files():
    files = []
    for folder in folders:
        files.append(glob.glob("/data/gazette-txt-files/" + folder + "/*.txt"))

    # convert 2D list into 1D
    formatted_list = []
    for file in files:
        for f in file:
            formatted_list.append(f)

    # 200 random text files
    return random.sample(formatted_list, 200)
</code></pre>

<p>For purposes like randomly selecting elements from a list, using <code>random.sample</code> suffices, true randomness isn't provided and I'm unaware if this is even theoretically possible. </p>

<p><code>random</code> (by default) uses a <a href="https://en.wikipedia.org/wiki/Pseudorandom_number_generator" rel="nofollow">Pseudo Random Number Generator</a> (PRNG) called Mersenne Twister (MT) which, although suitable for applications such as simulations (and minor things like picking from a list of paths), shouldn't be used in areas where security is a concern due to the fact that <a href="https://en.wikipedia.org/wiki/Pseudorandom_number_generator#Potential_problems_with_deterministic_generators" rel="nofollow">it is deterministic</a>.</p>

<p>This is why Python <code>3.6</code> also introduces <code>secrets.py</code> with <a href="https://www.python.org/dev/peps/pep-0506/" rel="nofollow">PEP 506</a>, which uses <code>SystemRandom</code> (<code>urandom</code>) by default and is capable of producing <a href="https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator" rel="nofollow">cryptographically secure pseudo random numbers</a>.  </p>

<p>Of course, bottom line is, that even if you use a PRNG or CPRNG to generate your numbers they're still going to be pseudo random.</p>

python python-3.x random