2
votes

I've been trying for days to try find a way to make random numbers in the logitech gaming software (LGS) scripts. I know there is the

math.random()
math.randomseed()

but the thing is i need a changing value for the seed and the solutions from others are to add a os.time or tick() or GetRunningTime stuff which is NOT supported in the LGS scripts. I was hoping some kind soul could help me by showing me a piece of code that makes pure random numbers. Because i don't want the pseudo random numbers because they are only random once. I need it to be random every time It runs the command. Like if i loop the math.randomI() one hundred times it will show a different number every time. Thanks in advance!

2
GetRunningTime seems to be supported. - Egor Skriptunoff

2 Answers

1
votes

Having a different seed won't guaratee you having a different number every time. It will only ensure that you don't have the same random sequence every time you run your code.

A simple and most likely sufficient solution would be to use the mouse position as a random seed.

On a 4K screen that's over 8 Million different possible random seeds and it very unlikely that you hit the same coordinates within a reasonable time. Unless your game demands to click the same position over and over while you run that script.

1
votes

This RNG receives entropy from all events.
Initial RNG state will be different on every run.
Just use random instead of math.random in your code.

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