Always random numbers

Hello again))

Is there any way to ensure that math.random function will always get different numbers?

i have this set up, but images often repeated, i’d like them to be different every time and not match

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

local shipTable = {
{img = “rocketShip1.png”, color = “green”},
{img = “rocketShip2.png”, color = “blue”},
{img = “rocketShip3.png”, color = “red”}

}

maxImages = 3
t = {}

for many=1, maxImages do

local i = math.random(#shipTable)

t[many] = display.newImage(shipTable[i].img)
t[many].color = shipTable[i].color
t[many].x = math.random(50,300)
t[many].y = math.random(50,300)

local function onTouch(event)

print(image.color)

end

t[many]:addEventListener(“touch”, onTouch)

end[/lua]

any help is appreciated [import]uid: 16142 topic_id: 17967 reply_id: 317967[/import]

Nope. But you can save the last math.random number and check it if it’s diffrent.

something like

local lastRandomNumber = 0  
  
local function random(min,max)  
 local r = math.random(min,max)  
 if r==lastRandomNumber then  
 return random(min,max,oldValue)  
 else  
 lastRandomNumber = r  
 return r  
 end  
end  
  
   
 for i=0,20 do  
 print(random(1,2))  
 end  
   

Edit: Don’t forget to save the last value returned by this random function somewhere! Edited the code therefore

Edit: Code works. Should return 1 2 1 2 1 2 … [import]uid: 13097 topic_id: 17967 reply_id: 68613[/import]

i tried it with my function and still object came out two of them same…
[lua]local lastRandomNumber = 0

local function random(min,max)
local r = math.random(min,max)
if r==lastRandomNumber then
return random(min,max,oldValue)
else
lastRandomNumber = r
return r
end
end

local shipTable = {
{img = “rocketShip1.png”, color = “green”},
{img = “rocketShip2.png”, color = “blue”},
{img = “rocketShip3.png”, color = “red”}

}

maxImages = 3
t = {}

for many=1, maxImages do

local i = random(1,3)

t[many] = display.newImage(shipTable[i].img)
t[many].color = shipTable[i].color
t[many].x = math.random(50,300)
t[many].y = math.random(50,300)

end[/lua]

did i make something wrong? [import]uid: 16142 topic_id: 17967 reply_id: 68618[/import]

you didn’t call the random function, but math.random :slight_smile: [import]uid: 13097 topic_id: 17967 reply_id: 68619[/import]

i did called “random” function in line 27: local i = random(1,3)
… [import]uid: 16142 topic_id: 17967 reply_id: 68622[/import]

Oh right. Sorry I’ve just skipped through your code. hmm… That’s odd.
Could you zip a little sample for me?
[import]uid: 13097 topic_id: 17967 reply_id: 68623[/import]

http://www.2shared.com/file/r4_dgfAr/codepiece.html

here [import]uid: 16142 topic_id: 17967 reply_id: 68625[/import]

Strange. Maybe because it’s looping before it can create the new number… Dunno…
You could also make a table from and fill it and afterwards shuffle it!

Here are 2 shuffle functions for you:

[code]
local function randomizeArray(arr)
for i = #arr, 2, -1 do – backwards
local r = math.random(i) – select a random number between 1 and i
arr[i], arr[r] = arr[r], arr[i] – swap the randomly selected item to position i
end
return arr
end
local function randomizeArrayAlt(arr)
local len = #arr
for i = 1, len, 1 do – backwards
local ran = math.random(1,len) – select a random number between 1 and i
local char = arr[ran]
arr[ran] = arr[i]
arr[i] = char
end

return arr
end
[/code] [import]uid: 13097 topic_id: 17967 reply_id: 68627[/import]

thanks for shuffling functions, it will come in handy
thanks for help, you’re awesome)

by the way, do you have a skype account? [import]uid: 16142 topic_id: 17967 reply_id: 68628[/import]

No Problem.

Too bad I couldn’t fix your problem :confused: Damnit!

Sure: search for xxxfanta :wink: [import]uid: 13097 topic_id: 17967 reply_id: 68629[/import]