这里使用了3个用户输入变量,一个为起始编号,另一个为结束编号,最后一个要显示多少个随机数。Python提供了一个随机模块来生成随机数。在这里,我们将随机函数与辐射函数一起使用。
n :: 10 s :: 20 e :: 40 [20, 35, 32, 27, 24, 35, 28, 31, 20, 28]
Randomno(s,e,n) Step 1 : input starting range(s), ending range(e) and number of elements needs to be appended(n). Step 2 : r= [] /*Python provides a random module to generate random numbers. Step 3 : for j in range(n) r.append(random.radient(s,e)) /*radiant is a method, it accepts two parameters.
# To generate random numbers and store in a list import random def randomno(s,e,n): ran = [] for i in range(n): ran.append(random.randint(s, e)) return ran # Driver Code n = int(input("How many Random Numbers want to disaply ::")) s = int(input("Enter Starting number ::")) e = int(input("Enter Ending number ::")) print(randomno(s, e, n))
输出结果
How many Random Numbers want to disaply :: 10 Enter Starting number :: 20 Enter Ending number :: 40 [20, 35, 32, 27, 24, 35, 28, 31, 20, 28]