math.random returns the same numbers

  • #1, z ke4Monday, 14. November 2016, 14:59 hodinky 8 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

    Zabiják klíčů

    810 Posts


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

    Imperátor

    7285 Posts

  • #3, z ke4Monday, 14. November 2016, 15:09 hodinky 8 years ago
    For example

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

    Zabiják klíčů

    810 Posts

  • #4, z afrlmeMonday, 14. November 2016, 16:22 hodinky 8 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

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

    ... 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

    Imperátor

    7285 Posts

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

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

    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.


    Zabiják klíčů

    810 Posts

  • #6, z afrlmeMonday, 14. November 2016, 17:27 hodinky 8 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

    Imperátor

    7285 Posts

  • #7, z ke4Monday, 14. November 2016, 17:38 hodinky 8 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


    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

    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?

    Zabiják klíčů

    810 Posts

  • #8, z afrlmeMonday, 14. November 2016, 17:46 hodinky 8 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

    Imperátor

    7285 Posts

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

    Zabiják klíčů

    810 Posts

  • #10, z afrlmeMonday, 14. November 2016, 17:54 hodinky 8 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())

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

    Imperátor

    7285 Posts

  • #11, 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