随机数生成器python

We can use Python random module to generate random numbers. We can use this module to generate random integers, floating-point numbers, or a sequence of random numbers.

我们可以使用Python 随机模块生成随机数。 我们可以使用该模块生成随机整数,浮点数或随机数序列。

1.用Python生成随机整数 (1. Generating Random Integer in Python)

Python random module randint() function can generate a random number between the given endpoints.

Python随机模块randint()函数可以在给定的端点之间生成一个随机数。

>>> from random import randint
>>>
>>> randint(1, 10)
6
>>> randint(1, 10)
8
>>> randint(1, 10)
2

Note that the randint() includes both endpoints while generating the random integer. If you don’t want the second endpoint to be included, then use randrange() function.

请注意,在生成随机整数时, randint()包括两个端点。 如果您不希望包含第二个端点,请使用randrange()函数。

>>> from random import randrange
>>>
>>> randrange(1, 10)
8
>>>

2.在Python中生成随机浮点数 (2. Generating Random Float in Python)

The random module has range() function that generates a random floating-point number between 0 (inclusive) and 1 (exclusive).

random模块具有range()函数,该函数生成介于0(含)和1(不含)之间的随机浮点数。

>>> import random
>>>
>>> random.random()
0.5453202789895193
>>> random.random()
0.9264563336754832
>>>

There is no separate method to generate a floating-point number between a given range. For that, just multiply it with the desired range.

没有给定范围之间生成浮点数的单独方法。 为此,只需将其乘以所需的范围即可。

Here is a simple program to generate a random floating-point number between 0 and 100.

这是一个简单的程序,用于生成0到100之间的随机浮点数。

>>> random.random() * 100
28.226855764270553
>>> random.random() * 100
41.844280268733115
>>>

3.从序列中选择一个随机数 (3. Chosing a Random Number from a Sequence)

We can also use the random module to select a random number from a sequence using the choice() method.

我们还可以使用choice模块使用choice()方法从序列中选择一个随机数。

>>> from random import choice
>>> nums = [1,3,5,7,9,11]
>>> choice(nums)
5
>>> choice(nums)
7
>>> choice(nums)
3
>>> choice(nums)
3
>>>

4.快速随机种子 (4. A quick word on Random Seed)

When we call random module functions, it initializes the Random class internally and uses the current system time to generate the seed value. This seed value is then used to generate random numbers.

当我们调用随机模块函数时,它将在内部初始化Random类,并使用当前系统时间生成种子值。 然后将此种子值用于生成随机数。

We can also set the random seed, in that case, the same sequence of random numbers will get generated every time.

我们还可以设置随机种子,在这种情况下,每次都会生成相同的随机数序列。

Let’s understand this behavior with a simple example.

让我们通过一个简单的例子来了解这种行为。

>>> import random
>>>
>>> random.seed(1000)
>>>
>>> random.random()
0.7773566427005639
>>> random.random()
0.6698255595592497
>>> random.random()
0.09913960392481702
>>>
>>> random.seed(1000)  # resetting the seed again to 1000
>>>
>>> random.random()
0.7773566427005639
>>> random.random()
0.6698255595592497
>>> random.random()
0.09913960392481702
>>>

Notice that the random numbers generated each time are following the same sequence. Unless you want something like this, try to avoid setting the random seed value.

请注意,每次生成的随机数都遵循相同的顺序。 除非您想要这样,否则请避免设置随机种子值。

翻译自: https://www.journaldev.com/31916/random-number-generator-in-python

随机数生成器python

随机数生成器python_Python中的随机数生成器相关推荐

  1. 随机数生成器 java_Java中的随机数生成器

    随机数生成器 java Today we will look at how to generate a random number in Java. Sometimes we need to gene ...

  2. python迭代器和生成器_python中迭代器和生成器。

    前言:很多python教程中,对python的解释不容易理解,本文记录自己的理解和体会,是对迭代器和生成器的初步理解. 迭代器: 迭代器的实质是实现了next()方法的对象,常见的元组.列表.字典都是 ...

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

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

  4. matlab 随机数权重,MATLAB中加权随机数

    R = randsample([1 2 3], N, true, [0.3 0.1 0.2]) a = 1:3; %# possible numbers w = [0.3 0.1 0.2]; %# c ...

  5. Java 产生随机数:Java 中产生随机数的方法及应用汇总

    文章目录 前言 一.System.currentTimeMillis() 方法 1.1.System.currentTimeMillis() 方法原理剖析 1.2.实现随机数加法程序 二.Math.r ...

  6. matlab 随机数有效数字,MATLAB中生成随机数方法总结

    好久没用MATLAB了,今天在利用MATLAB进行数据处理时,突然发现自己忘记了该如何产生自己需要的随机数形式,于是又查了一通资料.现对其进行一个简单的总结,供自己和大家以后参考: 1. randi ...

  7. Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom

    Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom 文中的 Random即:java.util.Random, ThreadLocalRandom 即 ...

  8. C语言中的随机数生成器

    在我们编写程序的时候,经常会需要电脑给我们随机生成一个整数,这个时候我们就需要一个随机数的生成器--rand().rand()为C语言中的函数,调用该函数需要加头文件#include<stdli ...

  9. Python中的随机数生成器模块(真/伪随机数)

    真随机数发生器(TRNG) 真随机数发生器会生成几乎无法预测的随机数,因为影响结果值变化的因素是物理环境的特征.例如,掷骰子将生成难以预测的随机值.但是骰子的数量限制为1到6.因此,几乎很难预测生成随 ...

最新文章

  1. 让Mac OS 10.x.x安装在Vmware虚拟机上!
  2. oc runtime
  3. 在win7下安装SQL sever2005
  4. 体验产品一 | 悦动圈VS咕咚竞品分析报告
  5. 如何把一个软件嵌入另一个软件_新增一个软件一个游戏
  6. asp.net 加载xml到menu
  7. MySQL导入导出远程访问命令
  8. adf4351使用方法_ADF:使用HTTP POST方法进行URL任务流调用
  9. HTML期末学生大作业-乒乓球网页作业html+css+javascript
  10. S5PV210体系结构与接口05:时钟系统编程
  11. win10如何解决浏览器出现“正在解析主机”的问题,很大原因是虚拟机,虚拟网卡,小米随身wifi导致的,DNS优选下载,
  12. 嵌入式基础面八股文——并发,同步,异步,互斥,阻塞,非阻塞的理解(2)
  13. GPS定位技术相关的毕业论文有哪些呢?
  14. 计算机开机今入dos系统,开机如何进入dos系统_如何进入纯dos系统
  15. 软件工程——软件需求分析
  16. 韩立刚计算机网络——第四章:网络层
  17. 2020switch电信最快的dns_求教电信宽带switch用哪个dns快
  18. R语言基本用法(主要为时间序列分析方面)
  19. 陶博士-选股思路-如何应用月线反转
  20. 如何选择正确的Node框架:Express,Koa还是Hapi?

热门文章

  1. 深入理解Java虚拟机2——内存管理机制及工具
  2. using关键字的用法以及作用
  3. Java匹马行天下之C国程序员的秃头原因
  4. 前端之CSS第二部分属性相关
  5. Eclipse下创建Spring MVC web程序--非maven版
  6. python爬虫--爬取豆瓣top250电影名
  7. 前端---二级级联下拉列表的实现
  8. hadoop深入学习之SequenceFile
  9. 使用SQL Server Management Studio 创建数据库备份作业
  10. NOI 题库 6264