math.random(), further!

  • Hi everyone!

    Today, we will take a peek into math.random()
    Even if you probably know about this, we will take a further peek in this tutorial. Scroll down! :P

    What is math.random()?
    math.random() is a LUA function that generates a pseudo-random integer(number). You can use it for lots of things, for example, to choose an "it" for a round of a murder game, to make a random daily gift, etc.

    Now let's see:

    math.random() with no arguments will generate a random number between 0 and 1 (with decimals!). This means you can get any number from 0 to 1(with decimals!). For example, 0.632468 .

    math.random() with 1 argument will generate a random number between 1 and the argument provided (without decimals!). For example, using math.random(70) will generate a random number between 1 and 70 (without decimals!), like it could be 39.

    math.random with 2 arguments will generate a random number between the first argument and the second (without decimals!). For example, using math.random (-67, 50) will generate a random number between -67 and 50 (without decimals!), like it could be -3.

    And what if I want to generate a random number with decimals that is not between 0 and 1?


    local disismynumberr = math.random (3,15) / 100 -- Write as many zeros as decimals you want.

    We can generate a random number and divide it. Or we can do math.random() and multiply it. As you may like.

    local disismynumberr = math.random () * 10 -- Same here. Zeros, zeros, zeros as much as ya'll fancy ;P

    PD: Beware about zeros, if you write too much you may not get a decimal number at the end!

    And we are done with the part about arguments on math.random()!

    BUT... We aren't done yet, don't leave!

    The thing that many developers don't know is that math.random() does NOT generate complete pseudo-random numbers at the first time!

    Lemme explain. On some operative systems, like OS, the first pseudo-random generated number (the first math.random()) is not completely random. So, when we use math.random() for the first time in our game, it could not work properly on some devices and it could generate a "not random" number at all.

    So, how do we fix this?

    It's easy. As normal, to get a number between 2 and 5, we would do this:

    local disismynumberr = math.random (2,5)

    But, as to ensure the number will be complete random, we must use a math.random() for no purpose before it.

    math.random() -- math.random() starting up
    local disismynumberr = math.random (2,5)

    Great! Now it will work!

    I hope you enjoyed this tutorial and that if you didn't knew this, I am happy you know do! Tell me your thoughts and give some upvotes if you liked it! Thanks! :3

    More about math.random():

  • Why not use this random? :P

  • @TheAlphaStigma

    I still prefer the classic. :P

  • No problem. :P

  • :P

    Let's spam :P

    :p :p :p


  • (':P'):rep(math.huge) :P

  • l0l
    iz dat gunn4 b a t4ng?

  • Treasure Random hunt Simulator!

  • Best of all, I'm getting more posts continuously talking xP

  • No problems here. :P

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.