Topic says it all. I’ve set out to create a random number generator for an ARRAY of numbers with no repeats. Not terribly advanced or anything like that, but I just wanted to make sure I have the best possible run-time. I realize something like this isn’t going to break the bank with app performance, it’s more of a curiosity for me because run-time analysis isn’t exactly my strong suit.
I *think* the run-time for this is O(n). Would that be safe to say? Can you guys think of any algorithms that would be more efficient? I tried googling this but didn’t come up with much. Thanks!
[lua]–Generates a random set of NUM numbers between LOW and HIGH with no repeats
–Returns array RANDOMS which contains the random set of numbers with no repeats
function randomsNonRepeat(low,high, num)
local numList = {}
local randoms={}
local temp
for i=low, high do
numList[#numList + 1] = i
end
for j=1, num do
temp=math.random(low, #numList)
randoms[#randoms+1] = numList[temp]
table.remove(numList, temp)
end
return randoms
end[/lua]
Thanks!
[import]uid: 52208 topic_id: 18726 reply_id: 318726[/import]


[import]uid: 58849 topic_id: 18726 reply_id: 72078[/import]
[import]uid: 52208 topic_id: 18726 reply_id: 72173[/import]