math.random returns the same numbers

  • #10, z ke4Monday, 14. November 2016, 17:56 hodinky 8 years ago
    I've just googled the same thing. It seems to work now. Well thanks for the help mate, i'm gonna finish the thing i was working on smile

    Zabiják klíčů

    810 Posts


  • #11, z afrlmeMonday, 14. November 2016, 18:01 hodinky 8 years ago
    No problem at all... at least we figured out a working solution in the end - even though the bloody math.random function should have been able to sort itself out (wonder why they don't implement the random seed thing into the math.random function itself so it can automatically generate random values each time it's called).

    Imperátor

    7285 Posts

  • #12, z ke4Monday, 14. November 2016, 18:48 hodinky 8 years ago
    Yeah that's how i supposed the fucntion should work.

    It's not finished. Should give you the idea what i'm doing here tough.
    (i have no idea how to turn off the variable fps mode in OBS, so it looks blury )

    Zabiják klíčů

    810 Posts

  • #13, z afrlmeMonday, 14. November 2016, 19:09 hodinky 8 years ago
    Looks pretty trippy! grin

    Anyway, here's a small workflow function you could use for generating a unique random value each time you call it...
    -- * function for rounding up decimals to a precise amount of places * --
    
    function round(num, idp)
    
       local mult = 10^(idp or 0)
    
       return math.floor(num * mult + 0.5) / mult
    
    end
    
    
    
    -- * function for generating a random value * --
    
    function rand(a, b)
    
      math.randomseed(os.time())
    
      -- + --
    
      if a == nil and b == nil then
    
        return math.random()
    
      elseif a ~= nil and b ~= nil then
    
       return math.random(a, b)
    
      else
    
        return math.random(a)
    
      end
    
    end

    usage examples...
    print( round(rand(), 2) ) -- random decimal value between 0.0 and 1

    
    print( rand(3,7) ) -- random value between 3 and 7

    print( rand(5) ) -- random value between 0 and 5

    It seems we can't return the current ms of the time value - only the current time & date in total amount of seconds. os.date on the other hand returns a formatted date & time stamp, so that's not much use as it's a string containing : & / etc.

    Imperátor

    7285 Posts

  • #14, z ke4Monday, 14. November 2016, 19:19 hodinky 8 years ago
    This is gonna be even much weirder mate! grin

    Thanks that could come handy. Why would we need the current time though?

    Zabiják klíčů

    810 Posts

  • #15, z afrlmeMonday, 14. November 2016, 20:15 hodinky 8 years ago
    math.randomseed() affects what math.random() generates apparently. It says something in the official Lua documentation website about updating it every second or so, but I figure you might as well just update it each time you call math.random() function instead as it's less taxing than running a loop to constantly update it, though I suppose editing the randomseed value once on game launch is probably enough.

    As for os.time(), it returns the current date/time in seconds, so instead of let's say "11/14/16 20:06:58" we get a integer value of total time instead, like so "1479150418".
    print( os.time() .. " = " .. os.date("!%c", os.time()) )

    https://i.gyazo.com/dd8488c6d50a4e18d6af0cd55a6ae5b9.png

    It's using that long integer number to calculate a random value against I guess...

    Imperátor

    7285 Posts

  • #16, z ke4Monday, 14. November 2016, 20:47 hodinky 8 years ago
    Sorry, i still don't understand why would you like to return the timestamp. Anyway i put math.randomseed() to my definiton script. I think it's enough.

    There's one flaw i didn't realize. The point is to avoid the hearts with cursor. Based on game.CurrentObject and mainloop. I didn't realize that the cursor has active only one point, that it's not active everywhere where the graphics is. I'll figure something out tomorow.

    Zabiják klíčů

    810 Posts

  • #17, z afrlmeMonday, 14. November 2016, 21:25 hodinky 8 years ago
    Are you offsetting the hearts with one of the move object action parts or the moveOBJ() workflow Lua function thing? If you are using one of those then the interaction polygon you assign to each heart object should move with the heart as well. I don't think scale affects the interaction polygon though as I think it remains the same size!?

    Simon created a function back in 4.2 which you can use to check if the cursor or something else is inside of the linked character, object or items interaction polygon. https://wiki.visionaire-tracker.net/wiki/IsPointInsidePolygon

    P.S: I didn't want to return the timestamp. I was just showing what os.time() was actually generating, which was a long number based on the current time & date, which we set as the randomseed to randomize how math.random() generated it's random numbers - or something along those lines... don't ask me as I hate math!

    Imperátor

    7285 Posts

  • #18, z ke4Tuesday, 15. November 2016, 12:33 hodinky 8 years ago
    Damn you're right. Neither Scale nor Rotaion affects the polygon. It's okay though i can make the sizes manually.
    I've tested that function you linked, but it always returns false to me.


    Zabiják klíčů

    810 Posts

  • #19, z afrlmeTuesday, 15. November 2016, 14:52 hodinky 8 years ago
    The isPointInsidePolygon function when used in combination with the mouse cursor position will only work based on the animation center (interaction position) you specify for each mouse cursor which as you know always tends to be at the tip of the arrow, so it will only work when the tip is inside of an objects polygon. You could probably do a bit of math to generate a rect polygon based on current cursor position. You would need to create a table though I think...
    
    local x, y
    
    
    
    function isRectInsidePolygon(obj, w, h)
    
         x = (game.ScrollPosition.x + getCursorPos().x); y = (game.ScrollPosition.y + getCursorPos().y)
    
         p1 = { x = (x - 1), y = (y - 1) }
      p2 = { x = (x + w), y = (y - 1) }
      p3 = { x = (x - 1), y = (y + h) }
      p4 = { x = (x + w), y = (y + h) }
       
    
        if not isPointInsidePolygon( p1, obj ) and not isPointInsidePolygon( p2, obj ) and not isPointInsidePolygon( p3, obj ) and not isPointInsidePolygon( p4, obj ) then
    
              startAction("Actions[no]")
    
         else
    
              startAction("Actions[yes]")
    
         end
    
    end
    isRectInsidePolygon(game.CurrentScene.SceneObjects["Rock"].ObjectPolygon, 15, 22)

    ... it's not very pretty mind. Also it's based on cursor default interaction position being at the top left which is the default arrowhead cursor direction in most os / apps / games.

    https://www.sendspace.com/file/j9gayx

    Imperátor

    7285 Posts

  • #20, z ke4Tuesday, 15. November 2016, 15:06 hodinky 8 years ago
    I'm aware of the issue.

    So this is what i have done. Always returns false though.
    a "is polygon inside of a polygon" thing.
    function objectDetect()
    
     Objects.OBJ_symbol.ObjectOffset = { x = ( getCursorPos().x - 35 ), y = ( getCursorPos().y - 39 )}
    
     for k,v in pairs(Objects.OBJ_symbol.ObjectPolygon) do
    
      local point = { x = v["x"], y = v["y"]}
    
      local poly = Objects.OBJ_heart_01.ObjectPolygon
    
      if isPointInsidePolygon(point, poly) then
    
       print( "true")
    
      end
    
     end	
    
    end
    
    
    
    registerEventHandler("mainLoop", "objectDetect")

    Zabiják klíčů

    810 Posts