math.randomseed()

类型 函数
math.*
返回值
修订版 发行版 2024.3703
关键词 randomseed
请参阅 math.random()

概述

为伪随机数生成器设置“种子”。同一个种子每次都会生成同一序列的随机数,这在测试时很有用,但通常更希望对每次运行采用不同的序列,例如将当前时间用作种子(见下文)。

语法

math.randomseed( seed )
seed(必需)

数字. 一个数字。

示例

可变种子
-- Produces a different sequence each time (assuming enough time between invocations)
math.randomseed( os.time() )
常量种子
-- Produces the same sequence of numbers
math.randomseed( 1234 )
print ( math.random(), math.random(), math.random() )  --> 0.31763056866714   0.416967588671   0.97426279353642
math.randomseed( 1234 )
print ( math.random(), math.random(), math.random() )  --> 0.31763056866714   0.416967588671   0.97426279353642