当前位置:网站首页>了解一下random库·1
了解一下random库·1
2022-06-11 17:53:00 【html_finder】
目录【篇·1】
目录
-2 Random类与random中的函数-random中函数与random.Random类中基本相同
-1 random.Random.__init__(self,x)[仅限制于Random类]
-2 random.Random.seed(self, a=None, version=2),random.seed(self,a=None,version=2)
-3 random.Random.random(self),random.random()
-1 random库的调用
import random-2 Random类与random中的函数-random中函数与random.Random类中基本相同
前言:random.Random中函数在random中可以被引用。
random.Random相当于创建一个单独的类。
-1 random.Random.__init__(self,x)[仅限制于Random类]
使用a=random.Random(x=10)创建一个Random类的调用。
·关于x参数:
类型
None, int, float, str, bytes, and bytearray.
用于seed()函数。见下。
-2 random.Random.seed(self, a=None, version=2),random.seed(self,a=None,version=2)
a参数,即上述x参数,作用为-指定随机数生成的种子,关系到随机数的生成方式。
【其实建议不需要指定】有默认值。
-3 random.Random.random(self),random.random()
生成0~1间的随机浮点数。
示例代码:
#coding=utf-8
import random
a=random.Random(1)
for i in range(10):
print(a.random())因为指定了seed,故生成方式将相同,每一次运行结果也应该会相同。
注意:是运行结果,不是生成结果!!!
输出部分:
0.13436424411240122
0.8474337369372327
0.763774618976614
0.2550690257394217
0.49543508709194095
0.4494910647887381
0.651592972722763
0.7887233511355132
0.0938595867742349
0.02834747652200631----------------------------------
如果你要阻止运行结果相同,只需要指定seed为None,即
#coding=utf-8
import random
a=random.Random()#或者a=random.Random(x=None)
for i in range(10):
print(a.random())-4 randint(a,b)-最常用
返回介于两数(a,b)间的随机值。
a可以等于b:返回a
a不能大于b:
Traceback (most recent call last):
File "C:\Users\*****\Desktop\MEME\IN\Random.py", line 8, in <module>
random.randint(100,0)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\random.py", line 338, in randint
return self.randrange(a, b+1)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\random.py", line 316, in randrange
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (100, 1, -99)返回的随机数值可以等于a,也可以等于b.
示例代码:
#coding=utf-8
import random
a=random.Random()
for i in range(10):
print(random.randint(0,3))
结果:【非唯一】
0
0
3
3
0
1
0
1
2
3-5 randrange(start,stop,step)
生成一个range对象:range(start,stop,step),再从中随机取值。
边栏推荐
- GB gb28181 protocol video platform easygbs adds or deletes offline channels
- Initial experience of MariaDB spider sharding engine
- 6-3 reading articles (*)
- 【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
- Install MariaDB 10.5.7 (tar package installation)
- Codeworks round 481 (Div. 3) [done]
- Ffmpeg hard codec inter QSV
- Hwang
- Global and Chinese markets for ultra high speed printers 2022-2028: Research Report on technology, participants, trends, market size and share
- [collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)
猜你喜欢

vulhub

Chorus translation

RadioGroup动态添加RadioButton
![[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration](/img/ae/9a0c300f2dcb03b05d737f14b0955f.jpg)
[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration

Ctfhub SQL Boolean blind annotation
![Codeworks round 481 (Div. 3) [done]](/img/60/01ed6180ccc4c99fe361d493525018.jpg)
Codeworks round 481 (Div. 3) [done]

【先收藏,早晚用得到】100个Flink高频面试题系列(三)

10 ways to reset any user password

Database lock and transaction isolation level

How to learn and self-study
随机推荐
NFT platform development NFT mall source code NFT mall development chain game development
Initial experience of MariaDB spider sharding engine
Système d'information sur les menaces à la sécurité des réseaux
【先收藏,早晚用得到】100个Flink高频面试题系列(一)
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (II)
【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
6-8 reading and writing of structured files 1
[foundation of deep learning] learning of neural network (3)
spawn ./ gradlew EACCES at Process. ChildProcess._ handle. onexit
Tidb CDC create task error unknown or incorrect time zone
【新手上路常见问答】关于项目管理
Online excel file parsing and conversion to JSON format
Three steps of ffmpeg CBR precise bitstream control
Bracket generation ---2022/02/25
sqli-labs通关嘿嘿~
Tle6389-2g V50's unique pwm/pfm control scheme has a duty cycle of up to 100%, forming a very low differential pressure - keshijin mall
10 ways to reset any user password
安全领域常规术语
ArrayList collection, object array
Hwang