How to make a random function without using any library?

Question:

I know that the way to produce random numbers from 0 to 1 (0.0 ≤ n < 1.0) can be done using a code like this in Python:

import random
print(random.random())

But I’m curious about how this function can be made and how it works. Is anyone who can explain how to make a random function from the scratch without using any library?

The result I want:

def getRandom():
  # return float between 0 and 1 (0 ≤ n < 1)

I have found several website sources, but I don’t get a good understanding of it.

Asked By: Jordy

||

Answers:

There is a whole field of research on it

https://en.wikipedia.org/wiki/Random_number_generation

In short, there are two types:

Pseudo-random number generation

This is the type we normally use in computing. The numbers are not really random: they follow a very long sequence that is repeatable, if you start from the same state.

However if you can somehow scramble the starting state to a truly random value, then the sequence of numbers meets most requirements of randomness for games etc, but not for cryptography.

True random number generation

This relies on getting information into the computer that is not predictable or repeating. This prevents the repeating pattern from being used by an attacker to subvert a cryptography scheme.

Typically we get this source randomness from physical sensors on the computer, e.g.

  • timing of mouse clicks and keypresses (which are unlikely to be repeated)
  • fine detail of values of temperature sensors inside the computer
  • other sensor information that contains noise

Sometimes these input sources are called "entropy". With a modest amount of entropy from true random number generation you can "seed" a pseudo-random number generator, well enough for normal non-cryptographic purposes.

Answered By: Eureka
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.