美文网首页python知识点程序员
随机数搞不明白?不存在的!

随机数搞不明白?不存在的!

作者: dmzlingyin | 来源:发表于2018-12-07 21:19 被阅读5次

python随机数

Python定义了一组用于生成或操作随机数的函数。这种特殊类型的函数用于许多游戏、彩票或任何需要生成随机数的应用程序。

随机数的操作

1、choice(): 这个函数用于在容器中生成一个随机数

2、randrange(beg,end,step):这个函数用来在给定的参数中生成随机数。这个函数接收三个参数,起始值,结束值,略过值。

下面看示例代码:

#导入'random'模块,进行随机操作
import random

#使用choice(),从给出的列表中随机选取一个数值

print('A random number from list is :',end='')
print(random.choice([1,2,3,4,5,6,7]))


#使用randrange()函数生成一个给定范围内的随机数。

print('A random number from range is :',end='')
print(random.randrange(20,50,3))

3、random():用来生成一个小于1,大于或等于0的随机浮点数

4、seed():这个函数映射一个特定的随机数和前面提到的seed参数。在种子值之后调用的所有随机数都返回映射的数字。

import random
#输出浮点数
print ("A random number between 0 and 1 is : ", end="") 
print (random.random()) 
  
#使用seed()生成一个随机种子 
random.seed(5) 
  
# 打印映射的随机数
print ("The mapped random number with 5 is : ", end="") 
print (random.random()) 
  
# 生成另外一个随机种子
random.seed(7) 
  
print ("The mapped random number with 7 is : ", end="") 
print (random.random()) 
  
#再一次用5作为参数
random.seed(5) 
  

print ("The mapped random number with 5 is : ",end="") 
print (random.random()) 
  
#再一次用7作为参数 
random.seed(7) 
  

print ("The mapped random number with 7 is : ",end="") 
print (random.random()) 

输出:

A random number between 0 and 1 is : 0.510721762520941
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237

5、shuffle():用来将整个列表中的数值打乱,重新排列。

6、uniform(a,b):用来生成给定范围的浮点数

import random 

# 初始化列表 
li = [1, 4, 5, 10, 2] 
  
# 打印重排之前的list 
print ("The list before shuffling is : ", end="") 
for i in range(0, len(li)): 
    print (li[i], end=" ") 
print("\r") 
  
# 重拍 
random.shuffle(li) 
  
# 打印重排之后的list
print ("The list after shuffling is : ", end="") 
for i in range(0, len(li)): 
    print (li[i], end=" ") 
print("\r") 

#使用uniform()生成给定范围内的浮点数
print ("The random floating point number between 5 and 10 is : ",end="") 
print (random.uniform(5,10)) 

输出:

The list before shuffling is : 1 4 5 10 2 

The list after shuffling is : 10 1 4 2 5 

The random floating point number between 5 and 10 is : 5.923955296706723

相关文章

  • 随机数搞不明白?不存在的!

    python随机数 Python定义了一组用于生成或操作随机数的函数。这种特殊类型的函数用于许多游戏、彩票或任何需...

  • 19.linux中使用随机数

    随机数和伪随机数 随机数是随机出现,没有任何规律的一组数列。 真正的完全随机的数列是不存在的,只是一种理想情况。我...

  • 搞不明白

    “集中精力支配自己的时间”? 嗯?怎么回事?时间都去哪了?

  • 搞不明白

    不论是书还是电影,真正到观众手里,总会删上一大段。原本丰满的故事,在理所应当的到达一个顶点时,“咔”一下被粗暴的剪...

  • 搞不明白

    你看吧,上课的时候你好好听讲,老师留的作业你不会作,这大把年纪了,总不能还一直去抄其他人的作业吧,一次两次可以,如...

  • 曾经(洛夫体)

    曾经 搞不明白 为什么要结婚 曾经 搞不明白 为什么要离婚 曾经 搞不明白 为什么不要离婚 水深火热之中 感情 总...

  • 区块链之密码学随机数

    一、随机数分类 真随机数其定义为随机样本不可重现。实际上只要给定边界条件,真随机数实际上并不存在,可是如果产生一个...

  • 2018-02-16

    真的搞不明白自己

  • 搞不明白自己

    跟老公冷战了,他不要跟我吵,让我冷静 说不清楚的心情 ,说的那么多好像他都对我心情改善都没帮助,还是会不舒服。 我...

  • 根本搞不明白

    电话公司的各种名目繁多,让人恼火 。生活本就烦乱,还要留意这日益昂贵的话费。和算也算不清楚的各种弯弯绕绕。办个业务...

网友评论

    本文标题:随机数搞不明白?不存在的!

    本文链接:https://www.haomeiwen.com/subject/etgxhqtx.html