怎么用python生成隨機數?
1本文需要加載的模塊是:random
2給出0到10之間的隨機整數:import randoma = random.randint(0,10)print(a)
3給出9到10之間的隨機實數:import randoma = random.uniform(9,10)print(a)
4從9、19、29、39、……、99之間,隨機選取一個實數:import randoma = random.randrange(9, 100, 10)print(a)
5從列表[5,6,7,8,9]里面,隨機選取一個數:import randoma = random.choice([5,6,7,8,9])print(a)
6從一個字符串里面,隨機選取一個字符:import randoma = random.choice("從一個字符串里面,隨機選取一個字符!")print(a)
7隨機打亂列表里面的字符順序:import randoma = ["p","q","r","s","t","p","q","r","s","t","p","q","r","s","t",]random.shuffle(a)print(a)
8從列表里面隨機選取9個數字:import randoma = range(3,100,2)b = random.sample(a, 9)print(b)