Python 生成隨機數


import random

x = int(input('Enter a number for x: '))  --隨機數最小值
y = int(input('Enter a number for y: '))  --隨機數最大值

n = int(input('How many numbers do you want to create? '))  --生成幾個隨機數
for i in range(1, n+1):
  z = random.randint(x, y)
  print(z)

 

運行結果:

>>> import random
>>> x = int(input('Enter a number for x: '))
Enter a number for x: 1
>>> y = int(input('Enter a number for y: '))
Enter a number for y: 100
>>> n = int(input('How many numbers do you want to create? '))
How many numbers do you want to create? 7
>>> for i in range(1, n+1):
    z = random.randint(x, y)
    print(z)


93
82
48
13
14
83
96
>>>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM