Python基础入门之seed()方法的使用

WBOY
Release: 2016-06-10 15:12:30
Original
2952 people have browsed it

 seed() 设置生成随机数用的整数起始值。调用任何其他random模块函数之前调用这个函数。
语法

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

seed ( [x] )

Copy after login

注意:此函数是无法直接访问的,所以需要导入seed模块,然后需要使用random静态对象来调用这个函数。
参数

  • x -- 这是下一个随机数的种子。如果省略,则需要系统时间,以产生下一个随机数。

返回值

此方法不返回任何值。
例子

下面的例子显示了seed()方法的使用。

#!/usr/bin/python
import random

random.seed( 10 )
print "Random number with seed 10 : ", random.random()

# It will generate same random number
random.seed( 10 )
print "Random number with seed 10 : ", random.random()

# It will generate same random number
random.seed( 10 )
print "Random number with seed 10 : ", random.random()

Copy after login

当我们运行上面的程序,它会产生以下结果:

Random number with seed 10 : 0.57140259469
Random number with seed 10 : 0.57140259469
Random number with seed 10 : 0.57140259469

Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!