math.random returns the same numbers

  • #1, by ke4Monday, 14. November 2016, 14:59 4 years ago
    Hi, I don't know why's that happening. The math.random() function returns always the same number ( numbers if i'm generating more than one ). Spooky.

    Do you have any idea what do to with that? Thanks

    Key Killer

    810 Posts


  • #2, by afrlmeMonday, 14. November 2016, 15:09 4 years ago
    How exactly are you using it?

    Imperator

    6842 Posts

  • #3, by ke4Monday, 14. November 2016, 15:09 4 years ago
    For example

    Objects.OBJ_heart_01.ObjectScale = (math.random(10, 20) / 10)
    1

    Key Killer

    810 Posts

  • #4, by afrlmeMonday, 14. November 2016, 16:22 4 years ago
    & it always returns the same value? Strange... maybe you could try creating a vs value & setting a random value with an action part then updating the object with that?
    math.random() + math.random(1) -- generate number between 0.0 and 2
    1

    or...
    math.random() + math.random() -- generate 2 random decimal values and add them together
    1

    ... though I'm guessing you always want it to be a scale somewhere between 1 to 2 right? In that case...
    1 + math.random() -- 1 + random decimal value between 0.0 and 1
    1

    Imperator

    6842 Posts

  • #5, by ke4Monday, 14. November 2016, 16:51 4 years ago
    Nice idea, i suppose i could do something like:

    Objects.OBJ_heart_01.ObjectScale = ( Values.randomScale.Int / 10 )
    1

    All the other stuff you wrote are doing the same, it returns always the same value. Regardless of how many times i run the game. It's the same number.

    However Scale is not the only value i want to set. So using vs values makes that unnecessarily complicated. I would like to get by just with lua script alone.

    Yeah i want a scale between 1 to 2. I guess I'm gona change it to .5 - 2 though. It should work with math.random(5,20) / 10 

    Btw the thing with vs value works fine.


    Key Killer

    810 Posts

  • #6, by afrlmeMonday, 14. November 2016, 17:27 4 years ago
    Really strange indeed. You could iterate through math.random() with a for loop a few times maybe?

    local v

    for i = 1, 10 do
    v = math.random() + math.random()
    -- print(i, v) -- test to see what it's generating
    if v < 0.5 then v = 0.5 elseif v > 2 then v = 2 end
    end

    Objects.OBJ_heart_01.ObjectScale = v
    1
    2
    3
    4
    5
    6
    7
    8
    9

    Imperator

    6842 Posts

  • #7, by ke4Monday, 14. November 2016, 17:38 4 years ago
    This are the numbers:

    1.4930587580684
    1.3433323584117
    0.74207844719507
    1.0750724414365
    0.99527908956351
    0.16935468460131
    1.6795612576328
    1.1507574967382
    0.63103710964268
    1.1513686351653
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10


    I run the game again and the numbers are:

    1.4930587580684
    1.3433323584117
    0.74207844719507
    1.0750724414365
    0.99527908956351
    0.16935468460131
    1.6795612576328
    1.1507574967382
    0.63103710964268
    1.1513686351653
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    So i don't understand grin

    Would you be willing to test the math.random() function at your computer to see if you are getting random numbers?

    Key Killer

    810 Posts

  • #8, by afrlmeMonday, 14. November 2016, 17:46 4 years ago
    I'm getting the same numbers returned in Sublime Text 3 as well each time, though not the numbers you are displaying.

    https://i.gyazo.com/67d3b56f3a3455b50f4a722df27bce12.png https://i.gyazo.com/d4603ab381aca49d1a33a2d4fe9f163f.png

    Imperator

    6842 Posts

  • #9, by ke4Monday, 14. November 2016, 17:49 4 years ago
    Hmm it seems it's not that random afterall grin

    Key Killer

    810 Posts

  • #10, by afrlmeMonday, 14. November 2016, 17:54 4 years ago
    It is... just we haven't adjusted how math.random works. I forgot. Try adding this line in some definition script somewhere or before you call a math.random() function...
    math.randomseed(os.time())
    1

    It seems to be working, but note that it will only generate new random values when the time is updated.

    Imperator

    6842 Posts

  • #11, by ke4Monday, 14. November 2016, 17:56 4 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

    Key Killer

    810 Posts

Write post