Random Uniform Python
In this tutorial, we will discuss the uniform() method present in the random module of Python and also how it can be used by importing NumPy.
The uniform() returns a floating-point number or a range of numbers between the specified limits.
The syntax for using uniform() is-
Here, "a" represents the upper bound & b represents the lower bound.
Let us have a look at some examples that demonstrates its usage in Python programs.
The following program illustrates how it can be used in a python program-
The random number between 8 and 12 is: 10.14646142251812
Let's understand what happened in the above program-
Let us have a glance at one more program where we have followed the same approach but provided the float values as upper and lower bounds.
The random number between 9.7 and 14.3 is : 11.521121715281813
The procedure is the same as the previous program but here we can observe it displays the required output even when we provide decimal values.
We all know that the NumPy module is used in Python to carry out different mathematical operations and because of the kind of in-built functions this module provides, our code becomes less complex and more efficient.
Let us see how we can use uniform() here,
Consider the program given below,
The resultant array is: [0.09310829 0.97165592 0.48385998 0.2425227 ]
It's time to know the explanation of the above program-
Now, let's check out another program-
The resultant array is: [[0.5488135 0.71518937 0.60276338] [0.54488318 0.4236548 0.64589411] [0.43758721 0.891773 0.96366276]] <class 'numpy.ndarray'>
Let's understand what happened here,
Finally, it's time to discuss the last program of this article,
The resultant array is: [53.52508358 57.01897669 54.6580309 53.44254684 50.89675079] <class 'numpy.ndarray'>
In this tutorial, we learned what is uniform() and how it can be used in various python programs.
Next TopicRelational Operators in Python
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week