需要用到两个函数:

(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也是变化的,这样就能保证每次产生的随机序列是不重复的(几率小)。

相关文章:

  • 2021-10-01
  • 2021-09-10
  • 2022-01-13
  • 2022-12-23
  • 2021-10-28
  • 2022-12-23
  • 2021-10-05
  • 2021-11-28
猜你喜欢
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
  • 2021-06-12
  • 2021-06-30
相关资源
相似解决方案