Programs that for provided input generate the same output every time are the deterministic type of programs. Deterministic programs are a good type of programs but some types of applications require unpredictability for example games. It's a pretty big challenge to make the application nondeterministic. However, the simple way to make it non-deterministic is to create pseudorandom numbers. These types of numbers are called pseudorandom numbers since they are generated using deterministic computation. When pseudorandom numbers are compared it is impossible to distinguish them from random. In Python programming language there is a random library that generates pseudorandom numbers. Inside the random library, there is a random function that will return the number in the range from 0.0 to 1.0 each time it's called. It is interesting to notice each time you generate a number using a random function from the random library it will return a number in a long type format.
Example 1 Generate 5 random numbers using random library and random function.
Solution First thing is to import the random library.
Example 2 Using randint function generate numbers in ranges 5 - 10, 20 - 60, 100-500, 1000 - 2000, and 10000-20000.
Solution First step is to import random library.
One of most commonly useful random functions is choice function. This function will randomly select the element from the list.
Example3 For a list of numbers [1,23,42,2,3,4,5,6,7] use choice function and show the selected element using print function.
Solution: Create a list and assign it to variable name lst1.
Example 1 Generate 5 random numbers using random library and random function.
Solution First thing is to import the random library.
import randomThe next step is to create for loop that will iterate five times to generate a random value using a random library and random function, assign it to variable name res (short for result), and finally print out the res variable.
for i in range(0,5,1):Output:
res = random.random()
print("res = {}".format(res))
res = 0.004982142417088831If we run the program again we will obtain different numbers.
res = 0.9879135988566512
res = 0.48766705436725544
res = 0.13378988187715124
res = 0.028625155543113734
res = 0.4469130507610821The random library contains other functions that handle random numbers besides random function used in the previous example. The randint function requires two parameters low and high and returns an inter between low and high values.
res = 0.5906871706018038
res = 0.8091787119834991
res = 0.37939426038366064
res = 0.22689141725030226
Example 2 Using randint function generate numbers in ranges 5 - 10, 20 - 60, 100-500, 1000 - 2000, and 10000-20000.
Solution First step is to import random library.
import randomNow the randint function will be used from the random library and ranges low and high given in the description of the example will be entered as the arguments of the function. Each of the randint functions with the range will be assigned to a variable and the chosen values will be shown as the output of the script using the print function.
a = random.randint(5,10)Output
b = random.randint(20, 60)
c = random.randint(100,500)
d = random.randint(1000,2000)
e = random.randint(10000, 20000)
print("a = {}".format(a))
print("b = {}".format(b))
print("c = {}".format(c))
print("d = {}".format(d))
print("e = {}".format(e))
a = 6In this example, the randint function was used to randomly select a number in a predefined range and this number was saved under a specific variable name. Each time the variable is called for example print("a = {}".format(a)) the output will be the same i.e. it will always show the value saved under that variable name. The different value would be generated if we typed in the following command print("{}".format(random.randint(5,10))) over and over again.
b = 50
c = 148
d = 1234
e = 16740
One of most commonly useful random functions is choice function. This function will randomly select the element from the list.
Example3 For a list of numbers [1,23,42,2,3,4,5,6,7] use choice function and show the selected element using print function.
Solution: Create a list and assign it to variable name lst1.
lst1 = [1,23,42,2,3,4,5,6,7]Then we have to randomly choose a list element and show it as the output of the program using the choice function and print function, respectively.
print("{}".format(random.choice(lst1)))Output:
5