Latest web development tutorials

Python3 seed () function

Python3 digital Python3 digital


description

seed () method to change the random number generator seed, you can call this function before calling other random module functions..


grammar

The following is the syntax seed () method:

import random

random.seed ( [x] )

Note: seed () is not directly accessible, you need to import the random module, and then call the method by random static objects.


parameter

  • x - change random number generator seed seed. If you do not understand the principle, you do not have to set special seed, Python will help you choose the seed.

return value

This function does not return a value.


Examples

The following illustrates the use of seed (() method Example:

#!/usr/bin/python3
import random

random.seed()
print ("使用默认种子生成随机数:", random.random())

random.seed(10)
print ("使用整数种子生成随机数:", random.random())

random.seed("hello",2)
print ("使用字符串种子生成随机数:", random.random())

After running the above example output is:

使用默认种子生成随机数: 0.9186252047469824
使用整数种子生成随机数: 0.5714025946899135
使用字符串种子生成随机数: 0.3537754404730722

Python3 digital Python3 digital