Random Uniform Python

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-

Output:

The random number between 8 and 12 is:  10.14646142251812

Explanation-

Let’s understand what happened in the above program-

  1. Since we have to use the method uniform(), we have imported the random module.
  2. After this, we have initialized the upper bound and the lower bound which are 8 and 12.
  3. Finally, we have passed these two values as the parameters in uniform().
  4. On executing this program, the expected output is displayed.

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.

Output:

The random number between 9.7 and 14.3 is :  11.521121715281813

Explanation-

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,

Output:

The resultant array is:  [0.09310829 0.97165592 0.48385998 0.2425227 ]

Explanation-

It’s time to know the explanation of the above program-

  1. Since we have to use the method uniform(), this time we have imported the NumPy module.
  2. The next step is to provide a value in random.seed() since it is used to initialize the random number generator.
  3. After this, we have initialized the values of size of the array, upper bound and the lower bound which are 4,0 and 1 respectively inside np.random.uniform().
  4. We have declared num_arr using np.random.uniform() because we are generating an array here.
  5. On executing this program, the expected output is displayed which is an array consisting of three values.

Now, let’s check out another program-

Output:

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'>

Explanation-

Let’s understand what happened here,

  1. Since we have to use the method uniform(), this time we have imported the NumPy module.
  2. The next step is to provide a value in random.seed() since it is used to initialize the random number generator.
  3. After this, we have initialized the values of size of the array(this time we have created a two-dimensional array), upper bound and the lower bound which are (3,3), 0 and 1 respectively inside np.random.uniform().
  4. We have declared num_arr using np.random.uniform() because we are generating an array here.
  5. On executing this program, the expected output is displayed which is an array consisting of three values and also the type of num_arr.

Finally, it’s time to discuss the last program of this article,

Output:

The resultant array is:  [53.52508358 57.01897669 54.6580309  53.44254684 50.89675079]
<class 'numpy.ndarray'>

Explanation-

  1. Since we have to use the method uniform(), this time we have imported the NumPy module.
  2. The next step is to provide a value in random.seed() since it is used to initialize the random number generator.
  3. After this, we have initialized the values of size of the array, upper bound, and the lower bound which are 5, 42, and 63(this time we have taken a definite range) respectively inside np.random.uniform().
  4. We have declared num_arr using np.random.uniform() because we are generating an array here.
  5. On executing this program, the expected output is displayed which is an array consisting of three values and also the type of num_arr.

Conclusion

In this tutorial, we learned what is uniform() and how it can be used in various python programs.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263368.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论