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!
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:
- How to Set PowerShell as Default Shell on Windows Terminal - Windows
- incorrect line ending: found carriage return (\r) without corresponding newline (\n) - Android
- Xcode 13 - unknown error compiling for iOS 15.0 but module has a minimum deployment target of iOS 15.2 - iOS
- Display ls command file sizes in KB (kilobytes) MB (megabytes) or GB (gigabytes) [Linux/macOS] - MacOS
- Android: Maps and Places using Maps SDK - Android
- How to Split a String by Space in Rust - Rust
- How to Center Align Image in Bootstrap - CSS
- How to Change Bootstrap Carousel Slide Speed - Bootstrap