Skip to content

Coincidences and programmed design

HOW TO PROGRAM COINCIDENCES

In this article, we will look at how to program coincidences in Python. First the theory: Real coincidences cannot be programmed in Python. This is referred to as "programmed randomness", which occurs according to a sequence. Here you can specify a range of numbers in which the random value should be generated.

The code in Python is random.randint(number,number)

The comma separates the numbers and acts as a hyphen (example from 0 - 6). We will now show you how to use the random numbers using the example of a game cube. A standard game cube has 6 sides, which are assigned the values 1 to 6. Now use the “randint“ function in your command. Enter random.randint(1,6) so that Python outputs integer values between 1 and 6. This way you can replace a cube with Python.

HOW TO PROGRAM GENERATIVE DESIGNS

If you want to generate several random numbers in order to use them in a generative design, for example, you must use the following code. The values here are only examples, theoretically any value is possible.

(PRNG CODE)

python
print()

import random

For i in range(0,50):
    r = random.randint(0,9)
    print(r, end=  " ")

This Python code performs the following actions:

  1. print(): An empty "print()" statement that only serves to output an empty line.

  2. import random: Imports the module "random", which provides functions for generating random numbers.

  3. For i in range(0,50): Starts a loop that is run through 50 times. A random number is generated and output in each iteration.

  4. r = random.randint(0,9): Generates a random number "r" in the range from 0 to 9 (including 0 and 9). This number is generated anew in each iteration of the loop.

  5. print(r, end= " "): Prints the generated random number "r", followed by a space (instead of a new line), since the parameter end is set to a space. As a result, all random numbers are output next to each other on the same line.

In total, this code produces an output of 50 random numbers between 0 and 9, which appear next to each other on the same line, each separated by a space.

UNDERSTANDING RANDOM NUMBER GENERATION IN PYTHON

There are different types of random number generators (RNGs), some of which are implemented in Python. The most common ones include:

  1. Pseudo-random number generators (PRNGs): These generators produce sequences of numbers based on a deterministic algorithm. Although they appear sufficiently random for most applications, they are not truly random since they are determined by their seed value. If the seed value is known, the subsequent numbers can be predicted. This can be a disadvantage, especially in security-critical applications.

  2. Cryptographically secure pseudo-random number generators (CSPRNGs): These generators produce number sequences considered to be cryptographically secure, meaning they are unpredictable even if a portion of the generated numbers is known. These are essential for applications such as cryptography and security.

  3. Physical random number generators: These generators use physical phenomena like thermal noise or radioactivity to generate random numbers. They provide a genuine source of randomness but may be more expensive or difficult to implement compared to PRNGs or CSPRNGs.

It's important to note that the choice of RNG depends on the specific application. For many applications, PRNGs may be sufficient, but in security-critical or cryptographic scenarios, CSPRNGs are essential.

To generate truly random numbers, physical random sources or cryptographically secure generators must be used. These provide genuine unpredictability crucial for many applications. In Python, cryptographically secure random number generators can be used from the secrets module, while physical random sources may require specialized hardware.

Thank you for taking the time to read this article. I hope you now have a better understanding of how random numbers can be programmed in Python and used in different use cases :)