简体   繁体   English

如何在此代码中两次生成随机数?

[英]How can I Generate the random number two times in this code?

In the below code, I generate random numbers and then taking the probability and create the graph.在下面的代码中,我生成随机数,然后取概率并创建图形。 now how can I make the graph two times like I want to generate the random numbers again and create graph again.现在我该如何制作图形两次,就像我想再次生成随机数并再次创建图形一样。

#!/usr/bin/env python3
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt

#Random Number Generating
x = np.random.randint(low=1, high=100, size=100000)


counts = Counter(x)
total = sum(counts.values())
d1 = {k:v/total for k,v in counts.items()}
grad = d1.keys()
prob = d1.values()
print(str(grad))
print(str(prob))
#bins = 20
plt.hist(prob,bins=20, normed=1, facecolor='blue', alpha=0.5)
#plt.plot(bins, hist, 'r--')
plt.xlabel('Probability')
plt.ylabel('Number Of Students')
plt.title('Histogram of Students Grade')
plt.subplots_adjust(left=0.15)

plt.show()

You can call the function as much as you need!您可以根据需要调用该函数!

import numpy as np
import matplotlib.pyplot as plt
from collections import Counter

def my_funct():
   np.random.seed(1223) # fixing the seed! but I don't think you need it

   #Random Number Generating
   x = np.random.randint(low=1, high=100, size=100000)
   counts = Counter(x)
   total = sum(counts.values())
   d1 = {k:v/total for k,v in counts.items()}
   grad = d1.keys()
   prob = d1.values()
   print(str(grad))
   print(str(prob))
   #bins = 20
   plt.hist(prob,bins=20, normed=1, facecolor='blue', alpha=0.5)
   #plt.plot(bins, hist, 'r--')
   plt.xlabel('Probability')
   plt.ylabel('Number Of Students')
   plt.title('Histogram of Students Grade')
   plt.subplots_adjust(left=0.15)

   plt.show()
#calling the function twice
my_funct()
my_funct()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM