In Python, we can generate the random numbers using the random() and uniform() functions.
import sys
from numpy import *
random():
This function generates the random numbers between 0 and 1
Examples:
# generating random numbers with random() function
x = random.rand()
y = round(x, 2)
z = int(x)
print('The Random float number is:', x)
print('The rounded Random rounded is:', y)
print('The Random integer number is:', z)
# Output:
The Random float number is: 0.9742596051564375
The rounded Random rounded is: 0.97
The Random integer number is: 0
# generating a range of random numbers with random() function
x = random.rand(5)
print('The range of Random float numbers :', x)
# Output:
The range of Random float numbers : [0.11325172 0.98133899 0.49293905 0.65075536 0.57226817]
# generating a 2D array(2x3) of random numbers
x = random.rand(2, 3)
print('The array of Random float number is:\n', x)
# Output:
The array of Random float number is:
[[0.48300304 0.52168378 0.54947105]
[0.19087592 0.31312512 0.08560706]]
uniform():
This function generates the random numbers between 0 and 1, and also between a range.
Examples:
x1 = random.uniform()
y1 = round(x1, 2)
z1 = int(x1)
print('The uniform Random float number is:', x1)
print('The rounded uniform Random number is:', y1)
print('The uniform Random integer number is:', z1)
# Output:
The uniform Random float number is: 0.5535500459174607
The rounded uniform Random number is: 0.55
The uniform Random integer number is: 0
# Generating random numbers between a range with uniform() function
x2 = random.uniform(10, 20)
y2 = round(x2, 2)
z2 = int(x2)
print('The uniform Random float number between a range:', x2)
print('The rounded uniform Random number between a range:', y2)
print('The uniform Random integer number between a range:', z2)
# Output:
The uniform Random float number between a range: 10.836354827177807
The rounded uniform Random number between a range: 10.84
The uniform Random integer number between a range: 10
#--------------------------------------------------------------Thanks--------------------------------------------------------------#
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.