🌐
Python documentation
docs.python.org β€Ί 3 β€Ί library β€Ί random.html
random β€” Generate pseudo-random numbers
February 23, 2026 - Source code: Lib/random.py This module implements pseudo-random number generators for various distributions. For integers, there is uniform selection from a range. For sequences, there is uniform s...
🌐
W3Schools
w3schools.com β€Ί python β€Ί ref_random_random.asp
Python Random random() Method
The random() method returns a random floating number between 0 and 1.
website that produces true random numbers based on atmospheric noise
Random.org (stylized as RANDOM.ORG) is a website that produces random numbers based on atmospheric noise. In addition to generating random numbers in a specified range and subject to a specified probability distribution, … Wikipedia
Factsheet
Type of site Web service
Available in English
Owner Mads Haahr
Factsheet
Type of site Web service
Available in English
Owner Mads Haahr
🌐
RANDOM.ORG
random.org
RANDOM.ORG - True Random Number Service
RANDOM.ORG offers true random numbers to anyone on the Internet. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.
🌐
Oracle
docs.oracle.com β€Ί javase β€Ί 8 β€Ί docs β€Ί api β€Ί java β€Ί util β€Ί Random.html
Random (Java Platform SE 8 )
2 weeks ago - If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random.
Top answer
1 of 6
6

Existing answers do a good job of addressing the question's specific, but I think it's worth mentioning a side issue: why you're particularly likely to want to pass an alternative "random generator" to shuffle as opposed to other functions in the random module. Quoting the docs:

Note that for even rather small len(x), the total number of permutations of x is larger than the period of most random number generators; this implies that most permutations of a long sequence can never be generated.

The phrase "random number generators" here refers to what may be more pedantically called pseudo-random number generators -- generators that give a good imitation of randomness, but are entirely algorithmic, and therefore are known not to be "really random". Any such algorithmic approach will have a "period" -- it will start repeating itself eventually.

Python's random module uses a particularly good and well-studied pseudo-random generator, the Mersenne Twister, with a period of 2**19937-1 -- a number that has more than 6 thousand digits when written out in decimal digits, as len(str(2**19937-1)) will confirm;-). On my laptop I can generate about 5 million such numbers per second:

$ python -mtimeit -s'import random' 'random.random()'
1000000 loops, best of 3: 0.214 usec per loop

Assuming a much faster machine, able to generate a billion such numbers per second, the cycle would take about 105985 years to repeat -- and the best current estimate for the age of the Universe is a bit less than 1.5*1012 years. It would thus take an almost-unimaginable number of Universe-lifetimes to reach the point of repetition;-). Making the computation parallel wouldn't help much; there are estimated to be about 1080 atoms in the Universe, so even if you were able to run such a billion-per-second generator on each atom in the Universe, it would still take well over 105800 Universe-lifetimes to start repeating.

So, you might be justified in suspecting that this worry about repetition is a tiny little bit of a theoretical, rather than practical, issue;-).

Nevertheless, factorials (which count the permutations of a sequence of length N) also grow pretty fast. The Mersenne Twister, for example, might be able to produce all permutations of a sequence of length 2080, but definitely not of one of length 2081 or higher. Were it not for the "lifetime of the Universe" issue, the docs' worry about "even rather small len(x)" would be justified -- we know that many possible permutations can never be reached by shuffling with such a pseudo-RNG, as soon as we have a reasonably long sequence, so one might worry about what kind of bias we're actually introducing with even a few shuffles!: -)

os.urandom mediates access to whatever sources of physical randomness the OS provides -- CryptGenRandom on Windows, /dev/urandom on Linux, etc. os.urandom gives sequences of bytes, but with the help of struct it's easy to make them into random numbers:

>>> n = struct.calcsize('I')
>>> def s2i(s): return struct.unpack('I', s)[0]
... 
>>> maxi = s2i(b'\xff'*n) + 1
>>> maxi = float(s2i(b'\xff'*n) + 1)
>>> def rnd(): return s2i(os.urandom(n))/maxi

Now we can call random.shuffle(somelist, rnd) and worry less about bias;-).

Unfortunately, measurement shows that this approach to RNG is about 50 times slower than calls to random.random() -- this could be an important practical consideration if we're going to need many random numbers (and if we don't, the worry about possible bias may be misplaced;-). The os.urandom approach is also hard to use in predictable, repeatable ways (e.g., for testing purposes), while with random.random() you need only provide a fixed initial random.seed at the start of the test to guarantee reproducible behavior.

In practice, therefore, os.urandom is only used when you need "cryptographic quality" random numbers - ones that a determined attacker can't predict - and are therefore willing to pay the practical price for using it instead of random.random.

2 of 6
4

The second argument is used to specify which random number generator to use. This could be useful if you need/have something "better" than random.random. Security-sensitive applications might need to use a cryptographically secure random number generator.

The difference between random.random and random.random() is that the first one is a reference to the function that produces simple random numbers, and the second one actually calls that function.

If you had another random number generator, you wanted to use, you could say

random.shuffle(x, my_random_number_function)

As to what random.random (the default generator) is doing, it uses an algorithm called the Mersenne twister to create a seemingly random floating point number between 0 and 1 (not including 1), all the numbers in that interval being of equal likelihood.

That the interval is from 0 to 1 is just a convention.

Find elsewhere
🌐
Dictionary.com
dictionary.com β€Ί browse β€Ί random
RANDOM Definition & Meaning | Dictionary.com
RANDOM definition: proceeding, made, or occurring without definite aim, reason, or pattern. See examples of random used in a sentence.
🌐
Cambridge Dictionary
dictionary.cambridge.org β€Ί dictionary β€Ί english β€Ί random
RANDOM | English meaning - Cambridge Dictionary
RANDOM definition: 1. happening, done, or chosen by chance rather than according to a plan: 2. strange or unusual…. Learn more.
🌐
RANDOM.ORG
random.org β€Ί integers
RANDOM.ORG - Integer Generator
This page allows you to generate random integers using true randomness, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.
🌐
Processing
processing.org β€Ί reference β€Ί random_
random() / Reference / Processing.org
Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. If only one parameter is passed to the function, it will return a float between zero and the value of the high parameter.
🌐
Wikipedia
en.wikipedia.org β€Ί wiki β€Ί Randomness
Randomness - Wikipedia
October 7, 2025 - In common usage, randomness is the apparent or actual lack of definite patterns or predictability in information. A random sequence of events, symbols or steps often has no order and does not follow an intelligible pattern or combination. Individual random events are, by definition, unpredictable, but if there is a known probability distribution, the frequency of different outcomes over repeated events (or "trials") is predictable.
🌐
Computer Hope
computerhope.com β€Ί jargon β€Ί r β€Ί random.htm
What Is Random?
June 14, 2025 - The term random refers to any collection of data or information with no determined order, or is chosen in a way that is unknown beforehand. For example, 5, 8, 2, 9, and 0 are single-digit numbers listed in random order.
🌐
Reddit
reddit.com β€Ί r/learnprogramming β€Ί how does random work??
r/learnprogramming on Reddit: How does random work??
July 23, 2024 -

I know this might seem like an incredibly stupid question, but how could a machine that takes orders of "1" as True and "0" as False be able to choose something random in its own, I've tried thinking about it for a long time and couldn't figure out how randomizing works (eg. Python random)

Top answer
1 of 36
188
You're basically right. Random libraries, to simplify it, generally use a (possibly truly random) seed, and an algorithm that generates pseudorandom numbers out of that seed. The random seed is usually collected from various sources of computer-unpredictable randomness, like variations in mouseclicks or ambient temperature or a number of other natural phenomena. These are then transformed into usable, useful random numbers via this process . Of course, you can also use deterministic seeds, or bad pseudorandom algorithms, which would give you less randomness in your random numbers. It's a huge field/topic
2 of 36
80
This isn't a stupid question. The answer is that random isn't actually random; it's using a deterministic function whose behavior is statistically indistinguishable from random noise. From the docs : Almost all module functions depend on the basic function random(), which generates a random float uniformly in the half-open range 0.0 <= X < 1.0. Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes. For actually random data, the operating system gathers entropy from various things - hardware RNGs , user activity like mouse clicks and keyboard input, and even webcam footage of lava lamps . This is often used to seed a pseudorandom generator like the Mersenne Twister.
🌐
Educative
educative.io β€Ί answers β€Ί how-to-use-nprandomrandom-in-python
How to use np.random.random() in Python
Being a powerful library, NumPy provides the np.random module which contains functions to generate random numbers.
🌐
Microsoft Learn
learn.microsoft.com β€Ί en-us β€Ί dotnet β€Ί api β€Ί system.random
Random Class (System) | Microsoft Learn
Console.WriteLine("Five random integers between 0 and 100:"); for (int ctr = 0; ctr <= 4; ctr++) Console.Write("{0,8:N0}", rand.Next(101)); Console.WriteLine(); // Generate and display 5 random integers from 50 to 100.
🌐
RANDOM.ORG
random.org β€Ί lists
RANDOM.ORG - List Randomizer
This page allows you to randomize lists of strings using true randomness, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-random-function
Python Random - random() Function - GeeksforGeeks
July 1, 2025 - The random() function in Python is used to generate a random floating-point number between 0 and 1.