编程语言
首页 > 编程语言> > Python02-01_伪随机数random

Python02-01_伪随机数random

作者:互联网

伪随机数random

1.random伪随机数的生成

random.randint()整形随机数

2.查看Python官方文档

  1. 打开IDLE,依次点击右上角的Help-->Python Docs(F1)

     

     

  2. 点击左上角的索引,然后输入想要查询的关键字-----(random)

     

     

  3. 点击第一个索引结果------random(module)

3.获取一个随机数

1 >>> random.randint(1, 10)
2 3
3 >>> random.randint(1, 10)
4 1
5 >>> random.randint(100, 10000)
6 3386
7 >>> random.randint(100, 10000)
8 152

 

4.重现伪随机数

 1 >>> x = random.getstate()
 2 >>> random.randint(1, 10)
 3 5
 4 >>> random.randint(1, 10)
 5 7
 6 >>> random.randint(1, 10)
 7 8
 8 >>> random.randint(1, 10)
 9 6
10 >>> random.randint(1, 10)
11 3
12 >>> random.randint(1, 10)
13 9
14 >>> random.setstate(x)
15 >>> random.randint(1, 10)
16 5
17 >>> random.randint(1, 10)
18 7
19 >>> random.randint(1, 10)
20 8
21 >>> random.randint(1, 10)
22 6
23 >>> random.randint(1, 10)
24 3
25 >>> random.randint(1, 10)
26 9

5.小游戏改进

 1 import random
 2 counts = 3
 3 answer = random.randint(1, 10)
 4 while counts > 0:
 5     temp = input("不妨猜一下我现在的心里想的是什么:")
 6     guess = int(temp)
 7     if guess == answer:
 8         print("你是我肚里的蛔虫嘛?")
 9         print("哼唧哼唧")
10         break
11     else:
12         if guess < answer:
13             print("小了")
14         else:
15             print("大了")
16         counts = counts - 1
17 print('游戏结束')

 

标签:10,01,randint,random,Python02,随机数,print,counts
来源: https://www.cnblogs.com/xujie-0528/p/13594093.html