Random Sequence of numbers

I need to generate a random sequence of numbers and store them in a table. But I’m not finding the best way to do so. I have some ideas but they all seem very inefficient when it comes to generating the sequence.

I’ve scoured the documents and forums but there’s not a lot of talk when it comes to unique sequences.

Any ideas?

Thanks, [import]uid: 10361 topic_id: 8052 reply_id: 308052[/import]

PS. The sequence will never be over 10 integers long. [import]uid: 10361 topic_id: 8052 reply_id: 28666[/import]

[lua]math.randomseed( os.time() )

local randInts = {}

for i=1,10 do
randInts[i]=math.random(10) – random number between 1 and 10
print("i = "…randInts[i])
end[/lua]

Hope that helps [import]uid: 34551 topic_id: 8052 reply_id: 28667[/import]

Wow… That was easier than expected. Thanks! I guess I need to gain a better understanding of iterators and how that works. I’ve been reading the lua.org/pil but coming up a little dry.

What if I want to make sure all the integers are unique?

Thanks again! [import]uid: 10361 topic_id: 8052 reply_id: 28681[/import]

So, any help on making sure they’re unique? I really need this to create a unique set. Please help!

Thanks, [import]uid: 10361 topic_id: 8052 reply_id: 28859[/import]

This probably isn’t the most elegant way to do this but you could do something like the following:

[lua]j={}

math.randomseed(os.time())
math.random(1,100) – throw away the first random value

for i=1,10 do
local u = false
while u == false do
u = true – assume the number will be unique
local n = math.random(1,100)
for k,v in ipairs(j) do
if v == n then – not unique, set u to false
u=false
end
end
if u == true then – number was unique, so add to table
table.insert(j,n)
end
end
end

for i,v in ipairs(j) do
print(v)
end[/lua] [import]uid: 34551 topic_id: 8052 reply_id: 29552[/import]

Thanks for the help… Carlos thought I might be able to get it accomplished without an additional table. So I’m not sure which way would be better. Currently I haven’t been able to get either way working. But now that I see your method, I may give that a shot.

Here’s what I had, trying to make this work “on the fly”, with no luck. :wink:

 local shapesOnScreen = {}  
  
 local function spawnRoundShapes()  
 while #shapesOnScreen \< numThisRound do  
 for i=1, numThisRound do   
 local n = math.random(#allShapes)  
  
 if(allShapes[n].used == 0) then  
 table.insert(shapesOnScreen, i, allShapes[n])  
 allShapes[n].used = 1  
  
  
 shapesOnScreen[i] = display.newImageRect(allShapes[n].img, 50,50)  
 shapesOnScreen[i].x = \_W/2; shapesOnScreen[i].y = \_H/3  
 postSpawnAction(shapesOnScreen[i])  
  
 else  
 i= i - 1  
 end  
 end  
 end   
 for i,v in ipairs(shapesOnScreen) do print(shapesOnScreen[i]) end  
 end  

Thanks a bunch for posting, It’s been 4 days since i’ve started trying to find a solution. Ever since they announced “Premium Support” I haven’t been able to get any answers… lol. [import]uid: 10361 topic_id: 8052 reply_id: 29555[/import]

This worked perfectly. Since I’m still learning, elegance is not my priority.

Thanks again Steve! [import]uid: 10361 topic_id: 8052 reply_id: 29557[/import]