The Wayback Machine - https://web.archive.org/web/20240524235948/https://www.geeksforgeeks.org/python-number-uniform-method/
Open In App

uniform() method in Python Random module

Last Updated : 15 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

uniform() is a method specified in the random library in Python 3.

Nowadays, in general, day-day tasks, there’s always the need to generate random numbers in a range. Normal programming constructs require a method more than just one word to achieve this particular task. In python, there’s an inbuilt method, “uniform()” which performs this task with ease and using just the one word. This method is defined in “random” module

Syntax : uniform(int x, int y)

Parameters :
x Specifies the lower limit of the random number required to generate.
y Specifies the upper limit of the random number required to generate.

Returns : Returns the generated floating point random number between lower limit and upper limit

 
Code #1 : Code to generate float random number.




# Python3 code to demonstrate
# the working of uniform()
  
# for using uniform()
import random
  
# initializing bounds 
a = 4
b = 9
  
# printing the random number
print("The random number generated between 4 and 9 is : ", end ="")
print(random.uniform(a, b))


Output:

The random number generated between 4 and 9 is : 7.494931618830411

Application :
There are many possible applications that can be thought of this function, some of the notable being generating random numbers in casino games, for lottery or custom games.
Below is the game that decided the winner on the basis of closeness to a certain value.

 
Code #2 : Application of uniform() – A Game




# Python3 code to demonstrate
# the application of uniform()
  
# for using uniform()
import random, math
  
# initializing player numbers
player1 = 4.50
player2 = 3.78
player3 = 6.54
  
# generating winner random number
winner = random.uniform(2, 9)
  
# finding closest 
diffa = math.fabs(winner - player1)
diffb = math.fabs(winner - player2)
diffc = math.fabs(winner - player3)
  
# printing winner
if(diffa < diffb and diffa < diffc):
    print("The winner of game is : ", end ="")
    print("Player1")
  
if(diffb < diffc and diffb < diffa):
    print("The winner of game is : ", end ="")
    print("Player2")
      
if(diffc < diffb and diffc < diffa):
    print("The winner of game is : ", end ="")
    print("Player3")


Output:

The winner of game is : Player2


Previous Article
Next Article

Similar Reads

numpy.random.uniform() in Python
With the help of numpy.random.uniform() method, we can get the random samples from uniform distribution and returns the random samples as numpy array by using this method. [caption width="800"]Uniform distribution[/caption] Syntax : numpy.random.uniform(low=0.0, high=1.0, size=None) Return : Return the random samples as numpy array. Example #1 : In
1 min read
Generate Random Numbers From The Uniform Distribution using NumPy
Random numbers are the numbers that cannot be predicted logically and in Numpy we are provided with the module called random module that allows us to work with random numbers. To generate random numbers from the Uniform distribution we will use random.uniform() method of random module. Syntax: numpy.random.uniform(low = 0.0, high = 1.0, size = None
1 min read
Python - Uniform Distribution in Statistics
scipy.stats.uniform() is a Uniform continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantiles loc : [optional]location parameter. Default = 0 scale :
2 min read
Python - Uniform Discrete Distribution in Statistics
scipy.stats.randint() is a uniform discrete random variable. It is inherited from the of generic methods as an instance of the rv_discrete class. It completes the methods with details specific for this particular distribution. Parameters : x : quantiles loc : [optional]location parameter. Default = 0 scale : [optional]scale parameter. Default = 1 m
2 min read
sympy.stats.Uniform() in Python
With the help of sympy.stats.Uniform() method, we can get the continuous random variable which represents the Uniform distribution. Syntax : sympy.stats.Uniform(name, left, right) Where, left and right are real number, -oo &lt; left, left &lt; right &lt; +oo Return : Return the continuous random variable. Example #1 : In this example we can see tha
1 min read
Os Module Vs. Sys Module In Python
Python provides a lot of libraries to interact with the development environment. If you need help using the OS and Sys Module in Python, you have landed in the right place. This article covers a detailed explanation of the OS and Sys Module including their comparison. By the end of this article, you will be able to easily decide which module suits
5 min read
Python Random Module
Python Random module generates random numbers in Python. These are pseudo-random numbers means they are not truly random. This module can be used to perform random actions such as generating random numbers, printing random a value for a list or string, etc. It is an in-built function in Python. List of all the functions Python Random ModuleThere ar
6 min read
Python Random - random() Function
There are certain situations that involve games or simulations which work on a non-deterministic approach. In these types of situations, random numbers are extensively used in the following applications: Creating pseudo-random numbers on Lottery scratch cardsreCAPTCHA on login forms uses a random number generator to define different numbers and ima
3 min read
PyQt5 QListWidget – Setting Uniform Item Sizes Property
In this article we will see how we can set uniform item sizes property of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. This property holds whether all items in the listvie
2 min read
PyQt5 QListWidget – Getting Uniform Item Sizes Property
In this article we will see how we can get uniform item sizes property of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. This property holds whether all items in the listvie
2 min read