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:
- [fix] zsh: command not found: git - Git
- Fix: error: legacy-install-failure Python pip/pip3 - PIP
- Java Split String by Spaces - Java
- [Solution] IDEA IntelliJ System.out.println function shortcut (sysout alternative for eclipse IDE) - HowTos
- Copy entire directory using Terminal Command [Linux, Mac, Bash] - Linux
- Python: How to add Progress Bar in Console with Examples - Python
- macOS: Remove Desktop or Documents Folder from iCloud Drive Syncing - MacOS
- Check Android Studio App is M1/M2 Chip based post installation - Android-Studio