@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.
@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,20do
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
@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.. 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.
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
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
if you need to random sort a table I would do as @ar2sawseen suggested: this, of course, assuming that I understood your needs
--create a tablelocal tmpTable ={}for i=1,10dotable.insert(tmpTable, i)--insert possible outcome numbers inside the table: at this time the table looks like tmpTable = {1,2,3,...10}endlocalfunction getNum()--get a random index from tmpTablelocal index =math.random(#tmpTable)--save the value of the number at given indexlocal 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 numberreturn num
end--get your numbers/ your table randomly sortedfor q=1, 10dolocal number = getNum()print(number)end
Comments
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.
you can use math.random and math.randomseed, getting your seed from os.timer() so that it's different on every app start.
EDIT: ar2sawseen was faster, and maybe had a better solution
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
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
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
It will get slower and slower as the list gets filled up. You could pregenerate the list.
https://deluxepixel.com
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
this, of course, assuming that I understood your needs