Python random seed() 函数

Python random seed() 函数

Python random seed() 方法改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数。

Python random seed() 语法

以下是 seed() 方法的语法:

import random

random.seed ( [x] )

我们调用 random.random() 生成随机数时,每一次生成的数都是随机的。但是,当我们预先使用 random.seed(x) 设定好种子之后,其中的 x 可以是任意数字,如10,这个时候,先调用它的情况下,使用 random() 生成的随机数将会是同一个。

注意:seed()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。

Python random seed() 参数

  • x : 改变随机数生成器的种子seed。如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed。

Python random seed() 返回值

本函数没有返回值。

Python random seed() 示例1

以下展示了使用 seed(() 方法的实例:

#!/usr/bin/python3
import random

random.seed()
print ("Generate random number using default seed:", random.random())
print ("Generate random number using default seed:", random.random())

random.seed(10)
print ("Generate random number using integer 100 seeds:", random.random())
random.seed(100)
print ("Using integer 10 seeds to generate random numbers:", random.random())

random.seed("apidemos",2)
print ("Using string seeds to generate random numbers: ", random.random())

输出:

Python random seed() 函数

Python random seed() 示例2

#!/usr/bin/python3
import random

random.seed(30)

# print a random number between 1 and 1000.
print(random.randint(1, 1000))

# if you want to get the same random number again then,
random.seed(30)
print(random.randint(1, 1000))

输出:

Python random seed() 函数

赞(3)
未经允许不得转载:极客笔记 » Python random seed() 函数

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
Python OS模块
Python os.chown方法Python os.write() 方法Python os.pardir 方法
Python String模块
Python String capitalize()方法Python String count()方法Python String center()方法Python String expandtabs()方法Python String index()方法Python String isalnum()方法Python String endswith()方法Python String encode()方法Python String find() 方法Python String decode()方法Python String isalpha() 方法Python String isdigit() 方法Python String islower() 方法Python String isnumeric() 方法Python String isspace() 方法Python String istitle() 方法Python String isupper() 方法Python String join() 方法Python String len() 方法Python String ljust() 方法Python String lower() 方法Python String lstrip() 方法Python String maketrans() 方法Python String max() 方法Python String min() 方法Python String replace() 方法Python String rfind() 方法Python String rindex() 方法Python String rjust() 方法Python String rstrip() 方法Python String isdecimal() 方法Python String splitlines() 方法Python String split() 方法Python String startswith() 方法Python String swapcase() 方法Python String strip() 方法Python String translate() 方法Python String title() 方法Python String zfill() 方法Python String upper() 方法
Python Math 模块
Python Math exp() 函数Python Math ceil() 函数Python Math floor() 函数Python Math fabs() 函数Python Math log10() 函数Python Math log() 函数Python Math pow() 函数Python Math modf() 函数Python round() 函数Python Math sqrt() 函数Python Math acos() 函数Python Math asin() 函数Python Math atan() 函数Python Math atan2() 函数Python Math cos() 函数Python Math degrees() 函数Python Math hypot() 函数Python Math radians() 函数Python Math sin() 函数Python Math tan() 函数
Python Random 模块
Python random choice() 函数Python random random() 函数Python random randrange() 函数Python random seed() 函数Python random shuffle() 函数Python random uniform() 函数
Python List 模块
Python List min() 方法Python List len() 方法Python List list() 方法Python List max() 方法