Is there a Lua random number function? menu

User Tag List

Results 1 to 5 of 5
  1. #1
    damdempsel's Avatar Member
    Reputation
    1
    Join Date
    Jan 2009
    Posts
    2
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a Lua random number function?

    I want to get a random number 1-100 and then have a response depending on what the number is. Is this possible?

    Is there a Lua random number function?
  2. #2
    Mr.Zunz's Avatar Contributor
    Reputation
    92
    Join Date
    Mar 2007
    Posts
    393
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


  3. #3
    damdempsel's Avatar Member
    Reputation
    1
    Join Date
    Jan 2009
    Posts
    2
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. I looked and didn't find anything. Instead of wasting a comment, why not just tell me what it is?

  4. #4
    Mr.Zunz's Avatar Contributor
    Reputation
    92
    Join Date
    Mar 2007
    Posts
    393
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by damdempsel View Post
    Yes. I looked and didn't find anything. Instead of wasting a comment, why not just tell me what it is?
    It's on the page of google's first result. Let me quote it for you

    [spoiler]
    math.random , math.randomseed

    math.random() generates pseudo-random numbers uniformly distributed. Supplying argument alters its behaviour:
    • math.random() with no arguments generates a real number between 0 and 1.
    • math.random(upper) generates integer numbers between 1 and upper.
    • math.random(lower, upper) generates integer numbers between lower and upper.


    > = math.random()
    0.0012512588885159
    > = math.random()
    0.56358531449324
    > = math.random(100)
    20
    > = math.random(100)
    81
    > = math.random(70,80)
    76
    > = math.random(70,80)
    75
    upper and lower must be integer. In other case Lua casts upper into an integer, sometimes giving math.floor(upper) and others math.ceil(upper), with unexpected results (the same for lower). The math.randomseed() function sets a seed for the pseudo-random generator: Equal seeds produce equal sequences of numbers.

    > math.randomseed(1234)
    > = math.random(), math.random(), math.random()
    0.12414929654836 0.0065004425183874 0.3894466994232
    > math.randomseed(1234)
    > = math.random(), math.random(), math.random()
    0.12414929654836 0.0065004425183874 0.3894466994232
    A good 'seed' is os.time(), but wait a second before calling the function to obtain another sequence! To get nice random numbers use:

    math.randomseed( os.time() )
    If Lua could get milliseconds from os.time() the init could be better done. Nevertheless, in some cases we need a controlled sequence, like the obtained with a known seed.
    But beware! The first random number you get is not really 'randomized' (at least in Windows 2K and OS X). To get better pseudo-random number just pop some random number before using them for real:

    -- Initialize the pseudo random number generator
    math.randomseed( os.time() )
    math.random(); math.random(); math.random()
    -- done. :-)
    -- This not exactly true. The first random number is as good (or bad) as the second one and the others. The goodness of the generator depends on other things. To improve somewhat the built-in generator we can use a table in the form:

    -- improving the built-in pseudorandom generator
    do
    local oldrandom = math.random
    local randomtable
    math.random = function ()
    if randomtable == nil then
    randomtable = {}
    for i = 1, 97 do
    randomtable[i] = oldrandom()
    end
    end
    local x = oldrandom()
    local i = 1 + math.floor(97*x)
    x, randomtable[i] = randomtable[i], x
    return x
    endend

    [4] : Why math.random() might give weird results on OSX and FreeBSD?
    ...The problem seems to be that when the seeds differ very little the first value of generated by BSD rand() also differ very little. This difference is lost when Lua converts the integer returned by rand() into a real number, effectively preserving only the high bits in the result. When you call math.random(1,100) from Lua, the low-bit difference vanishes and you see the same integer result.
    There is also lrandom[5] A library for generating random numbers based on the Mersenne Twister.

    [/spoiler]

    You obviously didn't search enough


  5. #5
    d3rrial's Avatar Contributor Authenticator enabled
    Reputation
    127
    Join Date
    Apr 2010
    Posts
    527
    Thanks G/R
    0/5
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if I remember correctly it was
    Code:
    math.random(1,100)

Similar Threads

  1. [LUA]random stat generator script
    By alj03 in forum WoW EMU Questions & Requests
    Replies: 0
    Last Post: 05-13-2010, 12:52 AM
  2. [Lua] Random Scripts.
    By Vision1000 in forum WoW EMU General Releases
    Replies: 3
    Last Post: 10-23-2009, 10:07 AM
  3. Randomizing Numbers +letters
    By omg_lol in forum Programming
    Replies: 6
    Last Post: 01-07-2009, 11:34 AM
All times are GMT -5. The time now is 10:02 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search