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

随机数运算

1.choice():此函数用于从容器生成1个随机数。

2.randrange(beg, end, step):此函数用于在其参数指定的范围内生成随机数。此函数有3个参数,开始编号(包含在生成中),最后一个编号(不包含在生成中)和step(在选择时跳过范围内的数字)。

# Python code to demonstrate the working of # choice() and randrange()    # importing "random" for random operations import random   # using choice() to generate a random number from a  # given list of numbers. print ("A random number from list is : ",end="") print (random.choice([1, 4, 8, 10, 3]))   # using randrange() to generate in range from 20 # to 50. The last parameter 3 is step size to skip # three numbers when selecting. print ("A random number from range is : ",end="") print (random.randrange(20, 50, 3)) 

输出:

A random number from list is : 4A random number from range is : 41

3.random() :此数字用于生成小于1且大于或等于0的float随机数。

4.seed():此函数使用提到的seed参数映射特定的随机数。在种子值之后调用的所有随机数都返回映射的数字。

# Python code to demonstrate the working of # random() and seed()    # importing "random" for random operations import random   # using random() to generate a random number # between 0 and 1 print ("A random number between 0 and 1 is : ", end="") print (random.random())   # using seed() to seed a random number random.seed(5)   # printing mapped random number print ("The mapped random number with 5 is : ", end="") print (random.random())   # using seed() to seed different random number random.seed(7)   # printing mapped random number print ("The mapped random number with 7 is : ", end="") print (random.random())   # using seed() to seed to 5 again random.seed(5)   # printing mapped random number print ("The mapped random number with 5 is : ",end="") print (random.random())   # using seed() to seed to 7 again  random.seed(7)   # printing mapped random number print ("The mapped random number with 7 is : ",end="") print (random.random()) 

输出:

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

5.shuffle():此函数用于随机排列整个列表。

6.uniform(a, b) :此函数用于在其参数中提到的数字之间生成浮点随机数。它包含两个参数:下限(包含在生成中)和上限(不包含在生成中)。

# Python code to demonstrate the working of # shuffle() and uniform()    # importing "random" for random operations import random   # Initializing list  li = [1, 4, 5, 10, 2]   # Printing list before shuffling print ("The list before shuffling is : ", end="") for i in range(0, len(li)):     print (li[i], end=" ") print("")   # using shuffle() to shuffle the list random.shuffle(li)   # Printing list after shuffling print ("The list after shuffling is : ", end="") for i in range(0, len(li)):     print (li[i], end=" ") print("")   # using uniform() to generate random floating number in range # prints number between 5 and 10 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 : 2 1 4 5 10 The random floating point number between 5 and 10 is : 5.183697823553464

python 随机数_Python中的随机数相关推荐

  1. python字符集_PYTHON 中的字符集

    Python中的字符编码是个老生常谈的话题,今天来梳理一下相关知识,希望给其他人些许帮助. Python2的 默认编码 是ASCII,不能识别中文字符,需要显式指定字符编码:Python3的 默认编码 ...

  2. python参数化_Python 中如何实现参数化测试的方法示例

    之前,我曾转过一个单元测试框架系列的文章,里面介绍了 unittest.nose/nose2 与 pytest 这三个最受人欢迎的 Python 测试框架. 本文想针对测试中一种很常见的测试场景,即参 ...

  3. kafka python框架_Python中如何使用Apache Avro——Apache的数据序列化系统

    了解如何创建和使用基于Apache Avro的数据,以实现更好,更有效的传输. 在这篇文章中,我将讨论Apache Avro,这是一种开源数据序列化系统,Spark,Kafka等工具正在使用该工具进行 ...

  4. python标准化_python中标准化

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! sdk 3.0 实现了统一化,各个语言版本的 sdk具备使用方法相同.接口调用方 ...

  5. python生成随机数代码_Python中产生随机数

    一.Python自带的random库 1.参生n--m范围内的一个随机数: random.randint(n,m) 2.产生0到1之间的浮点数: random.random() 3.产生n---m之间 ...

  6. python基本随机数生成函数有_Python中生成随机数的常用方法

    注意:random模块中的方法是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用方法. random.random() random()方法返回随机生成的一个实数(浮 ...

  7. python中如何使用随机数_Python中随机数的使用

    在Python中使用随机性的概述,仅使用内置于标准库和CPython本身的功能. Python随机数 生成介于0.0和1.0之间的随机浮点数 该random.random()函数在区间[0.0,1.0 ...

  8. python怎么输出表格中随机数_python 输出一个随机数

    优化MySchool数据库(四) 关于"无法附件数据库"过程的遇到的问题: 1.数据文件本身,具有访问权限的限制 ---- 选中 数据库文件所在的文件夹---->右键菜单(属 ...

  9. python如何生成随机数_python如何生成随机数

    如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与random模块中最常用的几个函数的关系,希望你会有所收获,以下就是这篇文 ...

最新文章

  1. VAE变分自编码器实现
  2. Edit Distance
  3. 能打造新型CPU的有机分子元件登Nature,用if语句攒出决策树,一个顶数千晶体管...
  4. iebook 发布到网站 独家秘诀
  5. 在VS中巧用文件系统来发布网站
  6. 使用BusyBox制作嵌入式Linux根文件系统
  7. python基础知识 os.path.join()
  8. 决策树算法 (CART分类树)
  9. hashcat进行rar密码破解可gpu运算
  10. 美团java后端_美团笔试题(Java后端5题2小时)(示例代码)
  11. 英语四级口语考试计算机考吗,听说四六级改革了,那英语四级考口语吗?
  12. A Piece of ODE
  13. 年薪 10 万的程序员,如何积累人生的第一个 100 万?
  14. python交易是什么意思_py交易是什么意思?
  15. Codeforces Round #828 (Div. 3)-赛后总结
  16. 零基础转行,你要不要去编程培训班?
  17. SOHO中国高管建“老鼠仓”吸钱 大企成空壳谁之责?
  18. 车载定位与轨迹服务系统
  19. slowfast代码实现和论文理解
  20. crond atd 定时处理

热门文章

  1. PHP面向对象常见符号总结($this- 、self ::)
  2. 在Linux服务器间数据相互复制
  3. Chrome 可移动绿色版
  4. OpenCV-扩充图像边界cv::copyMakeBorder
  5. python xampp mysql_让XAMPP支持Python及Django
  6. 网页读不出php语句,php - phpmyadmin显示代码而不是网页 - 堆栈内存溢出
  7. ios软件商店上架老被打回_一款APP上架苹果应用商店的流程,费用是多少?
  8. windows利用DOS窗口编译C++文件
  9. linux有名管道大小,Linux中的pipe与named pipe(FIFO),即管道和命名管道
  10. 学会这一招,轻松玩转 app 中混合应用自动化测试