-2

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jordy
  • 1,802
  • 2
  • 6
  • 25
  • 2
    Why do you want to do this instead of using a library? – ndc85430 Mar 05 '23 at 15:53
  • 3
    _curious about how this function can be made and how it works_ - Look at the Python source code... You might find it's not "one function" anyway to implement – OneCricketeer Mar 05 '23 at 15:53
  • I am just curious how it can happen. If I know how the random function works, I can build a function with one parameter and return different values for each execution. – Jordy Mar 05 '23 at 16:10
  • @OneCricketeer Where i can find the origin of the source code? – Jordy Mar 05 '23 at 16:11
  • https://github.com/python/cpython/blob/main/Lib/random.py – OneCricketeer Mar 05 '23 at 16:16
  • @Jordy you can find the code at their github or in the folder where you installed your python in the Lib subfolder – Copperfield Mar 05 '23 at 16:20
  • You can already build a function with parameters using libraries... The way it works internally depends on your operating system, and a seed value, so it's not going to be one function – OneCricketeer Mar 05 '23 at 16:20
  • Ok, I get it. Thanks for explanation guys – Jordy Mar 05 '23 at 16:32
  • Does this answer your question? [How to get random item from a list in python3 without any module](https://stackoverflow.com/questions/65509532/how-to-get-random-item-from-a-list-in-python3-without-any-module) – Peter O. Mar 05 '23 at 16:53
  • 1
    An example of a well-known algorithm which produces pseudo-random integer numbers, is simple enough to be programmed/understood by a beginner, and has quite good statistical properties: [MRG32k3a](https://rosettacode.org/wiki/Pseudo-random_numbers/Combined_recursive_generator_MRG32k3a). More details in this [paper](https://www.iro.umontreal.ca/~lecuyer/myftp/papers/streams00.pdf) by Pierre L'Ecuyer et al. – jpmarinier Mar 05 '23 at 19:47

1 Answers1

3

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.

ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26
  • Thanks for your clear explanation. But how can it be implemented in a single function? – Jordy Mar 05 '23 at 15:42
  • 3
    A [linear congruential generator](https://en.wikipedia.org/wiki/Linear_congruential_generator) can be implemented in just a few lines of code. Read the page for examples. – John Kugelman Mar 05 '23 at 16:03