Maximum number of correct objects (Table)

I need to change the following code, so it will only display a maximum number of correct objects (only 1 correct object should display every time, the rest should be “wrong”)… Right now it will display more than the max… of 1. Any suggestions? Thank you.

 for i = 1, #colorTable do if colorTable[i] ~= correctColor then wrongColorTable[#wrongColorTable+1] = colorTable[i] end end for objectNum = 1, totalObjects do local object if not((totalCorrectObjects == maxCorrectObjects) and totalObjects-objectNum == minCorrectObjects) then --if there's the minimum amount of correct objects if math.random(1,2) == 1 then --correct object object = display.newImageRect(correctColor[math.random(2, #correctColor)],205,155) totalCorrectObjects = totalCorrectObjects+1 --a correct object has been added object.name = "correct" else --wrong object local randomIndex = math.random(1, #wrongColorTable) object = display.newImageRect(wrongColorTable[math.random(1, #wrongColorTable)][math.random(2, #wrongColorTable[randomIndex])],205,155) object.name = "wrong" end else --display a correct object object = display.newImageRect(correctColor[math.random(2, #correctColor)],205,155) totalCorrectObjects = totalCorrectObjects+1 --a correct object has been added object.name = "correct" end

I believe the problem is this line: " if math.random(1,2) == 1 then --correct object"

You are running this test within a for loop, so roughly half of the time the random number will equal 1, and hence be a correct object.  I’m not sure how big the totalObjects table is, but if it’s more than 2 than this is where the issue is. 

Thanks for the reply JonPM

There are 26 totalObjects.  What would you recommend would be the best way to only display 1 correct object every time and 3 incorrect?

Thanks again

I believe the problem is this line: " if math.random(1,2) == 1 then --correct object"

You are running this test within a for loop, so roughly half of the time the random number will equal 1, and hence be a correct object.  I’m not sure how big the totalObjects table is, but if it’s more than 2 than this is where the issue is. 

Thanks for the reply JonPM

There are 26 totalObjects.  What would you recommend would be the best way to only display 1 correct object every time and 3 incorrect?

Thanks again