Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Generate random number without repetition — Gideros Forum

Generate random number without repetition

yubaroyubaro Member
edited January 2015 in General questions
Hi, I have not been able to solve this problem, can anyone help me?

Can anyone help me with the code in lua? I found examples only in java.

Thanks

Comments

  • @yubaro I suppose you might have a limit on numbers to generate, like from 1 to 100

    then what you can do is to create a table with values from 1 to 100, then get random table element and remove it from table, then again take random element from table, etc.

  • Thanks Arturs, you suggest me to do it with an enterFrame event?
  • piepie Member
    edited January 2015
    @yubaro I'm sorry I am not sure what do you mean by repetition :)
    you can use math.random and math.randomseed, getting your seed from os.timer() so that it's different on every app start.
    math.randomseed(os.timer()) 
    print(math.random())
    if instead repetition is about the risk to get the same number again inside a loop, like in
    for i=1,20 do 
     number = math.random(1,8)
    end
    I think you should "save" every number you get in a table, and then manually check if it already exists and act by consequence.

    EDIT: ar2sawseen was faster, and maybe had a better solution ;)
  • local rnd = {}
    for i = 1, 100 do
    rnd[i] = i
    end
    for i = 1, 100 do
    local rand = math.random(1,100)
    local tmp = rnd[i]
    rnd[i] = rnd[rand]
    rnd[rand] = tmp
    end
    Now You have a random table: rnd
  • thanks @pie, yes, not get the same number again inside a loop...you suggest me to do it with an enterFrame event?
  • piepie Member
    edited January 2015
    @yubaro It really depends on what you need and your "big scheme": I would try to avoid onEnterFrame event since it's time based and happens continuously; but I could be wrong.. :D in some situations it's the only way. :)

    I would do this:
    if you just need it to get all numbers once, use a for
    if you need to get a new number every now and then, use a function that does everything and call it only when it's needed
    if you need a continuous number generation use onEnterFrame. Just keep in mind that it is casted on every frame (30 or 60 times per second - according to your project settings).
    You could also use Timer class if you need more control.

    good luck :)
  • yubaroyubaro Member
    edited January 2015
    I want my output something like:

    local rand = math.random(1,10)
    ....
    --code--
    ....
    print(rand)

    output random:
    5
    10
    4
    9
    5..this not, other number
    6
    4
    5..this not, other number
    5..this not, other number
    1
    1..this not, other number
    2
    3
    8
    7

  • Create a table, generate a random number, if the table entry[the random number]==nil then table entry[the random number]=true else generate another random number until done.

    It will get slower and slower as the list gets filled up. You could pregenerate the list.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • yubaroyubaro Member
    edited January 2015
    My problem is I can not solve as generate another random number, until find a different one to that already obtained:

    Something, output random:
    5
    10
    4
    9
    5..this not, other number
    6
    4
    5..this not, other number
    5..this not, other number
    1
    1..this not, other number
    2
    3
    8
    7
  • piepie Member
    edited January 2015
    if you need to random sort a table I would do as @ar2sawseen suggested:
    this, of course, assuming that I understood your needs :D
     
    --create a table
    local tmpTable = {} 
    for i=1,10 do 
    table.insert(tmpTable, i) --insert possible outcome numbers inside the table: at this time the table looks like tmpTable = {1,2,3,...10}
    end
     
    local function getNum()
    	--get a random index from tmpTable
    	local index = math.random(#tmpTable)
    	--save the value of the number at given index
    	local num = tmpTable[index]
    	--remove index value (table.remove removes the value mantaining index integrity - which is needed to use # - lenght operator)
    	table.remove(tmpTable, index)
    	--return the number
    	return num
    end
     
    --get your numbers/ your table randomly sorted
    for q=1, 10 do
    local number = getNum()
    print(number)
    end
  • Thanks @pie, is perfect :D thanks all!
Sign In or Register to comment.