python隨機產生4個互不相等的隨機數


從0~9中隨機產生4個互不相等的數,

方法一:

import random

s=[]
while(len(s)<4):
    x=random.randint(0,9)
    if x not in s:
    s.append(x)
            
print(s)

方法二:

import random

result=[]
x=range(10)

for i in range(4):
    t=random.randint(i,9)
    temp=x[i]
    x[i]=x[t]
    x[t]=temp
    result.append(x[i])
    
print result

 


免責聲明!

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



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