To find a random number between 0 and 1 we will make use of the random.rand() function from the NumPy module.
Code Example 1: random.rand(1)
# Step 1: Import numpy library
import numpy as np
# Step 2: Use random.rand function and get an array
random_no = np.random.rand(1)
# Step 3: Print the first element of the array
print("Random Number Between 0-1 is :", random_no[0])
Output:
0.5152378042391298
Code Example 2: random.random()
#### use numpy to generate a random number between 0 and 1
# Step 1: Import numpy library
import numpy as np
# Step 2: use random.random() to generate random number between 0 and 1
random_no = np.random.random()
# Step 3: print the output
print(f"Random number between 0-1 is {random_no}")
Output:
Random number between 0-1 is 0.49419449525748516
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python-Programs,
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- 34: Traverse a List in Reverse Order - 1000+ Python Programming
- 22: Send Yahoo! Email using smtplib - SMTP protocol client using Python Program
- 35: Python Program to find the System Hostname
- 27: Measure Elapsed Time for a Python Program Execution
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 2: Print your name using print() function - 1000+ Python Programs
- 25: How to rename a file using Python Program
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- 23: Python Programs to concatenate two Lists
- 36: Python Program Convert Hex String to Integer
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- 32: Python Program to Find Square Root of a Number
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 11: Calculate Percentage - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- 28: Program to Lowercase a String in Python
- Program 1: Print Hello World! - 1000+ Python Programs
- 21: Program to Delete File or Folder in Python
- 29: Program to convert Python dict to dataframe
- 33: Python Program to find the current time in India (IST)
More Posts:
- SharePoint Server 2016 Preview installation error - This Product Key isn't a valid Microsoft Office 2016 Product Key. Check that you've entered it correctly. - SharePoint
- [Android] Sharing Multiple Images to Twitter App from your App - Twitter
- How to Change Bootstrap Carousel Slide Speed - Bootstrap
- How to fix PIP Install error: subprocess-exited-with-error - PIP
- How to remove Spring Boot Text Banner shown in Console when Application Starts - Java
- How to Find Downloaded Files on Windows 11 - Windows-11
- Java: Create Temporary Directory and File and Delete when application terminates - Java
- Fix Error Code: 80090030 in Microsoft Outlook/Teams - Microsoft