How to Generate Random Float numbers in Python using random and uniform() functions?

Posted in

How to Generate Random Float numbers in Python using random and uniform() functions?
vinaykhatri

Vinay Khatri
Last updated on December 14, 2024

    Python comes with a built-in random module to generate random numbers. The random module provides various methods and properties that can generate random integers as well as float numbers. In this tutorial, we will walk through two of the random module's methods, random() and uniform(), and learn how to generate random float numbers. So let's get started.

    Generate a Random float number between 0 and 1

    To generate a random float number between 0 and 1, we can use the random.random() function. The random.random() function does not accept any argument value and returns a random number between the interval [0.0,  1.0).

    Example 1

    Let's print 5 random float numbers between 0 to 1.0 using the random.random() function.

    import random
    
    for i in range(5):
        #print the random float number 
        print(random.random())
    

    Output

    0.6312659793400986
    0.9704219134108379
    0.00036539975574878536
    0.46369616712594597
    0.0903899194196851

    Example 2

    The random() function only generates a random float number between 0.0 and 1.0. To generate a random number from 0.0 up to n we have to multiply the generated random float number with n. Let's generate 5 random float numbers between 0.0 to n, using the random() function.

    import random
    
    n= 10
    
    for i in range(5):
        #print the random float number  between [0.0, 10.0)
        print(random.random()*10)
    

    Output

    0.5505126025099993
    7.220282428167762
    6.040232673473125
    0.26924706557892475
    7.222115542734079

    Example 3

    In the above example by multiplying the random number by N we can set the upper range for the random float number but the lower limit will remain 0. To get a random float number between two given interval or ranges we can use the following formula or the random.uniform(start, stop) function (that we will see in the next section).

     start+(stop-start) * random()

    Let's write a Python script that generates 5 random float numbers between 10 and 100

    import random
    
    start = 10
    
    stop = 100
    
    for i in range(5):
        random_number = start +(stop-start)*random.random()
        print(random_number)

    Output

    67.59361905855519
    37.40223222454098
    16.661157110370347
    30.848616703550597
    15.412873019184778

    Get a random float number within a given range using random.uniform() function

    To generate a random float number between two specified ranges, we can use the random.uniform(start, stop) function. This function accepts two arguments start and stop, and return a randomly generated floating point Number N, i.e., equal to and greater than the start, and equal and less than stop (start <= N <= stop) for start < stop . If the start is greater than stop it will return a float number N i.e., stop<= N <=start .

    Syntax

    random.uniform(start, stop)

    Example

    Let's write a Python script that generates 5 random floating-point numbers between 10, and 100

    import random
    
    start = 10
    stop  = 100
    for i in range(5):
        #print the random float number  between 10 and 100
        print(random.uniform(start, stop))
    

    Output

    79.07855385994228
    56.168706207632354
    24.18532011207759
    75.99995810143452
    88.35876424124737

    Example 2

    We can also generate negative random float numbers.

    import random
    
    start = -100
    stop  = -10
    for i in range(5):
        #print the random float number  between -100 and -10
        print(random.uniform(start, stop))
    

    Output

    -88.85369709089807
    -94.43232163361664
    -32.509935301182566
    -22.05378635574631
    -24.49232345044136

    Generate a random floating point number up to n decimal point

    In the above example, the randomly generated floating point numbers have more than 10 decimal places. To reduce it up to 2 or 3, we can use Python's round() function.

    Example

    import random
    
    start = 10
    stop  = 100
    for i in range(5):
        random_number = random.uniform(start, stop)
        #limit the floating point number precision upto 2 decimal places 
        print(f"Random Number {i}: ", round(random_number,2))
    

    Output

    Random Number 0: 63.27
    Random Number 1: 52.98
    Random Number 2: 97.51
    Random Number 3: 15.92
    Random Number 4: 49.38

    Generate a List of random float numbers

    Let's say you want to initialize a Python list with a fixed number of random floating-point numbers. How would you do that? To generate a list of random float numbers, we can use the random.uniform() function inside a for loop and append the generated random number to the empty list.

    Example

    import random
    
    start = 10
    stop = 100
    
    #empty list
    random_list = []
    size = 10
    
    for i in range(size):
        #generate random flaot number
        random_number = random.uniform(start, stop)
    
        #round the decimal numbers upto 2 places
        random_number = round(random_number, 2) 
    
        #append the number to the list
        random_list.append(random_number)
        
    print(random_list)

    Output

    [23.12, 28.09, 44.16, 81.88, 10.95, 74.03, 26.96, 92.63, 79.95, 18.03]
    

    Generate a Random list of unique float numbers

    In the above example, we see how we can generate a list of random float numbers, but there is a chance that the list may contain duplicate float numbers. To generate a list of unique random float numbers, we have to put more logic inside the loop while appending the randomly generated float number to the list.

    Example

    import random
    
    start = 10
    stop = 100
    
    #empty list
    random_list = []
    size = 10
    
    while size>0:
        #random number between 10 and 100
        random_number = random.uniform(10, 100)
    
        #if generated number is not in random_list
        #append the number to the list
        if random_number not in random_list:
            random_list.append(random_number)
            
            #decrement the size by 1
            size -= 1
        else:
            continue
    
    print("Your unique random list of float numbers is:", random_list)

    Output

    Your unique random list of float numbers is: [97.6, 72.03, 79.63, 33.04, 67.06, 82.74, 14.11, 42.25, 80.55, 33.82]

    The above program will always print a random list of unique float numbers.

    Random float numbers using Numpy

    Generate an array of random numbers using Numpy

    The numpy module has a dedicated class to create random number values. To generate a random array of float numbers using numpy, we can use the numpy's random.rand() function

    syntax

    import numpy as np
    np.random.rand(d0,d1,...dn)

    The random.rand() function returns a random specified shape (d0, d1, ..., dn) ndarray of float number between the interval [0,1).

    Example 1

    Create a one-dimensional array of random float numbers between the range 0 to 100.

    import numpy as np
    
    size = 10
    
    #one dimensional array from range 0.0 to 99.99
    array = np.random.rand(size)*100 
    
    print(array)

    Output

    [78.25982545 22.2798724 94.55801986 31.03170587 20.01110123 6.45872563
    33.63354877 74.94266594 77.21550284 38.55932404]

    The random.rand() function will only generate an array of random float numbers between 0 and 1. To increase the range from 1 to 100, we have to multiply the array with 100, as we have done in this example.

    Example 2

    Generate a two-dimensional array of random float numbers between the range 0 to 100.

    import numpy as np
    
    rows = 4
    columns = 4
    
    #one dimensional array from range [0, 100)
    array = np.random.rand(rows, columns)*100 
    
    print(array)

    Output

    [[62.15513195 34.60915347 37.93933137 81.01277555]
    [28.04085858 12.26928112 35.46122637 44.94658332]
    [94.50469169 73.17002235 19.991989 85.31803832]
    [11.89249045 29.61793772 71.28479511 66.80166957]]

    Conclusion

    In this Python tutorial, we learned the different techniques to generate random float numbers in Python. The first method we discussed was random.random() function that returns a random number between 0.0 to 1.0 where 1.0 is not included. But with some logic, we can also specify the start and stop ranges and use the random.random() function to generate a random number between two ranges. The other method we discussed was random.uniform(start, stop) function, which allows us to specify the range for the random float number.

    People are also reading:

    Leave a Comment on this Post

    0 Comments