2

几天来,我一直在尝试找到一种在罗技游戏软件 (LGS) 脚本中生成随机数的方法。我知道有

math.random()
math.randomseed()

但问题是我需要改变种子的值,其他人的解决方案是添加os.time or tick() or GetRunningTimeLGS 脚本不支持的东西。我希望某个善良的灵魂可以通过向我展示一段生成纯随机数的代码来帮助我。因为我不想要伪随机数,因为它们只随机一次。每次运行命令时,我都需要它是随机的。就像我循环 math.randomI() 一百次一样,它每次都会显示不同的数字。提前致谢!

4

2 回答 2

1

拥有不同的种子并不能保证您每次都有不同的数字。它只会确保您每次运行代码时都没有相同的随机序列。

一个简单且最可能足够的解决方案是将鼠标位置用作随机种子。

在超过 800 万个不同可能的随机种子的 4K 屏幕上,您不太可能在合理的时间内点击相同的坐标。除非您的游戏要求在运行该脚本时一遍又一遍地单击相同的位置。

于 2019-03-19T07:32:05.297 回答
1

这个 RNG 从所有事件中接收熵。
每次运行的初始 RNG 状态都会有所不同。
只需在您的代码中 使用random而不是。math.random

local mix
do
   local K53 = 0
   local byte, tostring, GetMousePosition, GetRunningTime = string.byte, tostring, GetMousePosition, GetRunningTime

   function mix(data1, data2)
      local x, y = GetMousePosition()
      local tm = GetRunningTime()
      local s = tostring(data1)..tostring(data2)..tostring(tm)..tostring(x * 2^16 + y).."@"
      for j = 2, #s, 2 do
         local A8, B8 = byte(s, j - 1, j)
         local L36 = K53 % 2^36
         local H17 = (K53 - L36) / 2^36
         K53 = L36 * 126611 + H17 * 505231 + A8 + B8 * 3083
      end
      return K53
   end

   mix(GetDate())
end

local function random(m, n)  -- replacement for math.random
   local h = mix()
   if m then
      if not n then
         m, n = 1, m
      end
      return m + h % (n - m + 1)
   else
      return h * 2^-53
   end
end

EnablePrimaryMouseButtonEvents(true)

function OnEvent(event, arg)
   mix(event, arg)  -- this line adds entropy to RNG
   -- insert your code here:
   --    if event == "MOUSE_BUTTON_PRESSED" and arg == 3  then
   --       local k = random(5, 10)
   --       ....
   --    end
end
于 2019-03-19T08:28:26.350 回答