I need sequance of numbers from 1 to 5 in random order, for instance 4,2,1,5,3. How to do it in Lua language? [import]uid: 32980 topic_id: 6191 reply_id: 306191[/import]
math.randomseed(os.time()) – (somewhere near top of code, not in a loop)
local no = math.random(1,5)
For more info see…
http://developer.anscamobile.com/reference/index/mathrandom
http://developer.anscamobile.com/reference/index/mathrandomsed
[import]uid: 6981 topic_id: 6191 reply_id: 21246[/import]
If you want each number to appear uniquely, it’s a little more work:
[lua]-- assume randomseed was set at some point
local sequence, limit = {}, – desired length of sequence
for i=1,limit do
local pos = math.random(limit)
local n = 1; repeat
if sequence[n] then pos = pos + 1 end
n = n + 1
until n >= pos
sequence[pos] = i
limit = limit - 1
end[/lua]
This should randomly distribute integer values from 1-limit across integer keys from 1-limit in the table sequence. [import]uid: 12678 topic_id: 6191 reply_id: 21570[/import]
Yes, I want each number to appear uniquely. Thank you, alestane. [import]uid: 32980 topic_id: 6191 reply_id: 21653[/import]
What am I doing wrong here?
[lua]local sequence, limit = {8}, – desired length of sequence
for i=1, limit do
local pos = math.random( limit )
local n = 1; repeat
if sequence[n] then pos = pos + 1 end
n = n + 1
until n >= pos
sequence[pos] = i
limit = limit - 1
end[/lua]
Terminal Reads:
Syntax error: .../RandomNumbers/main.lua:36: unexpected symbol near 'for'
Also, how do I call these 8 individual random numbers?
I want to eventually have an “if” statement that says something like:
[lua]if value == 2 then
local disp = display.newImage( “image” … randmNum … “.png”, 0, 0, true)
– I want to … randmNum … to populate on it’s own
end[/lua]
I am trying to use this as a tool to load 16 different cards for a matching game. [import]uid: 7850 topic_id: 6191 reply_id: 25292[/import]
bump [import]uid: 7850 topic_id: 6191 reply_id: 39540[/import]
As far as your syntax error goes, it looks like you want to change
local sequence, limit = {8},
to
local sequence, limit = {}, 8
As far as getting the table to your eventual use code, you’ll need to pass it back from a creation function, or save it as a field in some data structure, or *shudder* save it in a global variable.
Assuming that it’s still named sequence when it reaches your other code, it would look something like
[lua]for i, randmNum in ipairs(sequence) do
if value == 2 then
local disp = display.newImage( “image” … randmNum … “.png”, 0, 0, true)
end
end[/lua] [import]uid: 12678 topic_id: 6191 reply_id: 39547[/import]
Use a shuffle algorithm on the table {1,2,3,4,5}
-- see: http://en.wikipedia.org/wiki/Fisher-Yates\_shuffle
function shuffle(t)
local n = #t
while n \> 2 do
-- n is now the last pertinent index
local k = math.random(n) -- 1 \<= k \<= n
-- Quick swap
t[n], t[k] = t[k], t[n]
n = n - 1
end
return t
end
myTable = shuffle( {1,2,3,4,5} )
[import]uid: 61132 topic_id: 6191 reply_id: 39549[/import]
I still can not get this to work…
There is nothing random about it
[code]
math.randomseed( os.time() )
function shuffle(t)
local n = #t
while n > 2 do
– n is now the last pertinent index
local k = math.random(n) – 1 <= k <= n
– Quick swap
t[n], t[k] = t[k], t[n]
n = n - 1
end
return t
end
nums = shuffle({})
local sequence, limit = {},9
for i=2,limit do
local pos = math.random(limit)
local n = 1; repeat
if sequence[n] then
pos = pos + 1
end
n = n + 1
until n >= pos
sequence[pos] = i
limit = limit - 1
print ( limit )
table.insert(nums, limit)
end
print (table.concat(nums, ", ")) [/code]
OUTPUT:
8
7
6
5
4
3
2
1
8, 7, 6, 5, 4, 3, 2, 1
Anyone have any ideas?
[import]uid: 7850 topic_id: 6191 reply_id: 40376[/import]
You can’t pass an empty table to a shuffle() function and expect anything good. It needs something to shuffle! Try the following:
math.randomseed( os.time() )
function shuffle(t)
local n = #t
while n \> 2 do
-- n is now the last pertinent index
local k = math.random(n) -- 1 \<= k \<= n
-- Quick swap
t[n], t[k] = t[k], t[n]
n = n - 1
end
return t
end
nums = shuffle({1,2,3,4,5})
nums should come out with a randomized sequence.
[import]uid: 61132 topic_id: 6191 reply_id: 40377[/import]
works 100% perfect!
Thanks so much for the help! [import]uid: 7850 topic_id: 6191 reply_id: 40378[/import]