In: Computer Science
Using Python, generate 100 random numbers ranging from 1 to 5, then plot them using matplotlib or Seaborn as a histogram
Code:
import matplotlib.pyplot as plt
#importing matplotlib module
import random
#importing random module to generate random numbers
def plothist():
#creating a function to generate histogram
number = [random.randint(1,5)for i in
range(100)]
#generating 100 random number in range of
1,5
print(number)
#printing the random numbers
plt.hist(number)
#ploting the histogram of the random
numbers
plt.show()
#displaying the histogram
plothist()
Explanation:
We have used matplotlib to generate the histogram.
We have imported matplotlib and random modules in the program.
Then declared a function named "plothist()"
Then generated 100 random numbers between the range of 1 and 5
using .hist() we have generated a histogram of the random numbers
using .show() we displayed the histogram
Screenshot of the Code:
Screenshot of the Output: