Logitech Gaming Software lua 脚本代码——如何生成纯随机数

我试了好几天在 Logitech Gaming Software (LGS) 脚本中生成随机数但一直找不到办法。我知道有以下这些函数:

math.random()
math.randomseed()

但问题是,我需要一个可变的种子值,而其他人的解决方案都是添加os.timetick()或者GetRunningTime之类的东西,而这些在 LGS 脚本中都不被支持。 我很希望有位善良的人能向我展示一些生成纯随机数的代码。因为我不想要伪随机数,因为它们仅仅是一次性的。我需要每次运行命令时都是不同的随机数。例如,如果我重复运行 math.random() 100次,每次都会显示不同的数字。 提前谢谢!

点赞
用户2858170
用户2858170

将随机种子设置为鼠标位置

使用不同的随机种子并不能保证每次得到不同的随机数。它只能确保每次运行代码时不会得到相同的随机序列。

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

在4K屏幕上,有超过800万个可能的随机种子,很难在合理的时间内命中相同的坐标。除非您的游戏要求在运行该脚本时不断单击相同的位置。

2019-03-19 07:32:05
用户6834680
用户6834680

这个 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)  -- 用于取代 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)  -- 这一行添加额外的熵到 RNG 中
   -- 在此处插入你的代码:
   --    if event == "MOUSE_BUTTON_PRESSED" and arg == 3  then
   --       local k = random(5, 10)
   --       ....
   --    end
end
2019-03-19 08:28:26