DEV Community

Tarush Arora
Tarush Arora

Posted on

What are Non-Deterministic Programs: Exploring Python Example

Level: Beginner

Regardless of the programming language, whenever we write a computer program, we expect a particular output for a given input. For instance, if you input two number, 10 and 5 for an addition program, you expect 15 as the total answer. You can run the program as many times you want, and whenever these two values will be inputted, output will remain same.

When the output is constant for a particular input, the program processing it is known as deterministic. In addition, such program also follows the same set of commands every time to process each input.
But, when random values are provided as output, even after going through same set of commands, the program is called as non-deterministic. These kind of programs mostly offer pseudorandom numbers as output.

Further, determinism is considered as a good thing, as output is stable. But, in some cases, such for gaming applications, random outputs are required. And for such purposes, you need non-deterministic programs.

Python example for Non-Deterministic Programs

To create a non-deterministic program, we’ll be using Python programming language and in-built function called *“random()”. *

The random() function is configured to provide random values between 0.0 and 1.0. Whenever this function is called, a list of values is provided as output. We are going to use a for-loop to output only the ten values.

Code:

import random

for i in range(10):
    x = random.random()
    print(x)
Enter fullscreen mode Exit fullscreen mode

Run the program multiple times and analyze the output.

Output #1:

Output 1

Output #2:

Output 2

Output #3:

Output 3

Top comments (1)

Collapse
 
bluedream0801 profile image
Declan

How are you
currently, we are going to build new coin marketplace similar pump.fun or base.fun.
If you have good experience with MERN Stack, I want to collaborate with you