Results 1 to 9 of 9

Thread: Generate a random number with Lua

  1. #1

    Default Generate a random number with Lua

    I looked through the on-line manual but couldn't find it. So can it be done?
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  2. #2

    Default

    OK found it:
    Code:
    r = math.random(12)
    Returns a random number between 1 & 12.
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  3. #3

    Default

    The strange thing is that NR always returns '1' the first time it's executed. So as a workaround I just entered the above line of code twice in succession in my function.
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  4. #4

    Default

    mmm

    After closer inspection it seems that when you close and Open NR Lua always returns the exact same sequence of numbers. So they are not random after all.

    Does anyone know how to create a truly random number using Lua?
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  5. #5
    Join Date
    May 2002
    Location
    CT
    Posts
    6,559

    Default

    Is this on PPC, Windows, or both?

  6. #6
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    Quote Originally Posted by Jlee
    mmm

    After closer inspection it seems that when you close and Open NR Lua always returns the exact same sequence of numbers. So they are not random after all.

    Does anyone know how to create a truly random number using Lua?
    You need to seed the RNG - Programming in Lua recommends
    Code:
    math.randomseed(os.date("%d%H%M%S"))
    --Rob

  7. #7

    Default

    both
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  8. #8

    Default

    Thanks Rob.

    So do I just put that code in my function before the match.random bit?
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  9. #9
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    You only want it to be called the once - so I'd probably put it in OnCCFLoad()
    --Rob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •