Randomize a number, new number every time

How can I make a function that:

  1. Randomizes a number (math.random)
  2. Makes sure it has never been used before
  3. Goes to 11 after 10 iterations

The second step is the trickiest.

I have:
[lua]i = 0
local function choose()
result = math.random(1,10)
i = i + 1
if i < 10 then
–DO SOMETHING HERE to make sure the random number is not the same as the last one
end
end[/lua]

What is the best way to going about this? I was thinking maybe add all result values to a table and search it in the do something block.
thank you,
Scott

[import]uid: 59735 topic_id: 29781 reply_id: 329781[/import]

Seems like what you were thinking would work fine! [import]uid: 147305 topic_id: 29781 reply_id: 119477[/import]

You need to seed the random number generator with some changing value.

By default, the random number generator **MAY** be seeded with 0 on devices . On the simulator it grabs whatever value was in the memory that’s used which may not be zeroed out.

Add this line of code to the top of your main.lua file:

math.randomseed(os.time())  

[import]uid: 19626 topic_id: 29781 reply_id: 119478[/import]

No need to search the table. you can test against the incoming value instantly by recording the table and testing for the value at that value as index

while (#myTable < 11) do
local newVal = random(1, 50)
if (myTable[newVal])then
skip stuff
else
myTable[newVal] = true
end
end

[import]uid: 63787 topic_id: 29781 reply_id: 128592[/import]

just make sure you have more than 10 values possible or you’ll never finish the loop! [import]uid: 63787 topic_id: 29781 reply_id: 128593[/import]

No need to search the table. you can test against the incoming value instantly by recording the table and testing for the value at that value as index

while (#myTable < 11) do
local newVal = random(1, 50)
if (myTable[newVal])then
skip stuff
else
myTable[newVal] = true
end
end

[import]uid: 63787 topic_id: 29781 reply_id: 128592[/import]

just make sure you have more than 10 values possible or you’ll never finish the loop! [import]uid: 63787 topic_id: 29781 reply_id: 128593[/import]