2

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!

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64

2 Answers2

2

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.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • 1
    When a game starts, mouse pointer is usually set to the center of the screen during game engine initialization. So, mouse is not sufficient. – Egor Skriptunoff Mar 19 '19 at 08:21
  • @EgorSkriptunoff well that would depend on when you run that script right? It's not the game's start that runs the script but some Logitech input event. I mean we're not trying to encrypt defense communication here. so using 2 mouse coordinates to calculate a random seed looks fine to me. – Piglet Mar 19 '19 at 08:28
  • 1
    User could move his mouse to a corner of the screen. – Egor Skriptunoff Mar 19 '19 at 08:29
  • I guess that `os.time()` will do same job while being harder to trick by user. – val - disappointed in SE Mar 26 '19 at 15:43
  • @val os.time() is not available that's the whole reason for the question... – Piglet Mar 26 '19 at 18:02
1

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
Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23