lua 中随机数产生

需要用到两个函数:

(1)math.randomseed(N):  接收一个整数N作为随机序列种子

(2)math.random([n, [m]]): 这个函数有三种用法,分别是不跟参数,此时产生(0,1)之间的随机浮点数;有一个参数n,产生1到n之间的整数;有2个参数n和m,产生n到m之间的随机整数。

最常用的方法是

math.randomseed(os.time())

for i=0, 10 do
    local n = math.random(10)
    print(n)
end

math.randseed(N)中N也是变化的,这样就能保证每次产生的随机序列是不重复的(几率小)。

猜你喜欢

转载自www.cnblogs.com/rohens-hbg/p/9002273.html