Problem with math.random() .. need help

Hello

I hope someone can help me:

I ve 2 texts

local x= { 200, 60, 20, } local testo1 = display.newText("Rosso" , 40 , 170 , "HoboStd" , 24) testo1:setFillColor(0 , 200 , 0 ) gruppo:insert(testo1) local testo2 = display.newText("Blu" , 40, 170, "HoboStd" , 24) testo2:setFillColor(200 , 0 , 0 ) gruppo:insert(testo2) function skip(event) if event.phase == "began" then testo1.x = x[math.random(1,3) testo2.x = x[math.random(1,3) end end Runtime:addEventListener("touch" , skip)

But in this way it is possible that the 2 texts have the same x coordinates and I want to avoid this. How can i do this still using the math.random function??

I SOLVED  THANKS ANYWAY :)))

Some of this might just be that you’re randomly choosing between 3 x-coords … so 33% of the time, they’re gonna have the same value.

And you don’t show any math.randomseed(…) call … so it’s also possible that since you’re using the same random-number sequence each time, this might just be a “bad” sequence (“bad” in the sense that it happens to give the same value twice in a row).

[lua]

local function shuffle(t)

    local rand = math.random; assert(t, “table.shuffle() expected a table, got nil”)

    local iterations = #t; local j

    for i = iterations, 2, -1 do

        j = rand(i); t[i], t[j] = t[j], t[i] – function to shuffle a table

    end

end

local x= {

200,

60,

20,

}

shuffle(x)

local testo1 = display.newText(“Rosso” , 40 , 170 , “HoboStd” , 24)

testo1:setFillColor(0 , 200 , 0 )

gruppo:insert(testo1)

local testo2 = display.newText(“Blu” , 40, 170, “HoboStd” , 24)

testo2:setFillColor(200 , 0 , 0 )

gruppo:insert(testo2)

function skip(event)

if event.phase == “began” then

testo1.x = x[1]

testo2.x = x[2]

end

end

Runtime:addEventListener(“touch” , skip)

[/lua]

Yes I know this, in fact i wanted a tip to solve this trouble but I ve found a method by my self … problem solved

thanks anyway

Some of this might just be that you’re randomly choosing between 3 x-coords … so 33% of the time, they’re gonna have the same value.

And you don’t show any math.randomseed(…) call … so it’s also possible that since you’re using the same random-number sequence each time, this might just be a “bad” sequence (“bad” in the sense that it happens to give the same value twice in a row).

[lua]

local function shuffle(t)

    local rand = math.random; assert(t, “table.shuffle() expected a table, got nil”)

    local iterations = #t; local j

    for i = iterations, 2, -1 do

        j = rand(i); t[i], t[j] = t[j], t[i] – function to shuffle a table

    end

end

local x= {

200,

60,

20,

}

shuffle(x)

local testo1 = display.newText(“Rosso” , 40 , 170 , “HoboStd” , 24)

testo1:setFillColor(0 , 200 , 0 )

gruppo:insert(testo1)

local testo2 = display.newText(“Blu” , 40, 170, “HoboStd” , 24)

testo2:setFillColor(200 , 0 , 0 )

gruppo:insert(testo2)

function skip(event)

if event.phase == “began” then

testo1.x = x[1]

testo2.x = x[2]

end

end

Runtime:addEventListener(“touch” , skip)

[/lua]

Yes I know this, in fact i wanted a tip to solve this trouble but I ve found a method by my self … problem solved

thanks anyway