In this tutorial, we will explore various methods and techniques for generating random numbers between 0 and 1. Doing so can allow you to get a great sample size of random values with low but very accurate precision.
And with that, let’s dive into it.”
Method 1 – Uniform Function
One of the most popular and probably easiest methods for generating a random number between 0 and 1 is the uniform().
The function is part of the random module that is built-in Python allowing you to fetch random values.
The function syntax is as shown below:
The function takes two arguments, a and b, with a representing the lowest value and b being the highest value.
For example, the code below shows how to generate a random number between 0 and 1.
print(uniform(0,1))
The program above is simple. We start by importing the uniform function from the random module.
We then use the function and specify zero as the lowest range and 1 being the highest value. This means that any value that gets generated is in that range.
Running the code above should return a random number between 0 and 1, as shown:
0.033120898443304236
As you can see, the random value we get from the function is a floating-point value.
Example 2 – Rounding Off
Ok. We have discovered that we can use the uniform() function to generate a random value between 0 and 1. However, we might need the returned value to have a specific precision.
This is where the round() function comes into assistance. We can use this function to specify the number of decimals we wish to get from the random value.
An example code is as shown:
print(round(uniform(0,1),3))
The code above allows us to generate a random value between 0 and 1 with a precision of 3 decimal places.
0.376
Example 3 – Generate Multiple Random Numbers
We can also generate a set of random numbers by wrapping the target range inside a for loop.,
For example, let’s say we want to generate 15 random numbers between 0 and 1. We can do the following:
for i in range(15):
print(uniform(0,1))
Here, you can see we use the range operator to fetch 15 random numbers. The resulting output is as shown:
0.5040968831729273
0.3457666715512162
0.449675199490307
0.652515835157846
0.8492446773560791
0.6407341841806988
0.5899676800951469
0.7723663363955782
0.6008153018769883
0.7744139450147812
0.8687276520510405
0.23189906589942322
0.1929730516770748
0.07403079330277496
Simple.
Method 2 – Python Randint() Function
As you can probably guess, the random module is specially designed to perform randomization operations in Python.
Hence, it offers another great function that we can use to generate random numbers between 0 and 1.
The function’s syntax is as shown:
The function takes two parameters and returns a random number in the range of the parameters, a and b.
The example below shows how to use this function to generate a random number between o and 1.
print(randint(0,1))
The code above should return an integer value between 0 and 1. One of the drawbacks of this function is that the return value is either 0 or 1. This gives you a smaller pool of values with which to work.
For example:
for i in range(10):
print(randint(0,1))
The code above should return a set of zeros and ones as shown:
0
0
1
1
1
0
1
0
1
Method 3 – Python Random Function
Yes, the third method we are going to use is from the random module. Surprisingly, the function is called random().
The syntax is as shown below:
The function itself takes no parameter but returns a random floating-point number in the range of 0.0 and 1.0.
This is a perfect function as we can just invoke it as shown:
for i in range(5):
print(random())
We can run the program above and get five random numbers:
0.8935659971732424
0.719979289496354
0.4984679841334766
0.7029698134701271
0.9824591264466329
Conclusion
That’s it for this one. In this tutorial, we discussed the various methods you can use to generate a random number between 0 and 1.
Thanks for reading & Happy coding!!