# Randomly fill an array

I have a 2D array that needs to be intialized with each cell being a random color (1 of 6)
here is the routine I use at the moment

`````` for x in(0...maxGRIDX) {
for y in(0...maxGRIDY) {
let clr = enumCOLOR(rawValue:Int.random(in: 1..<clr))!
sgBoard[x,y] = tileRecord(color:clr,selected:false,highlight:false)
}
}
``````

just choose a random value for each cell…

HOWEVER… in some situations I need to do the same thing, but insure that no where in the array are there 3 cells in a row (or column) that are the same color…

One idea was to populate it (as above), then find 3 in a row cells, and recolor them until no 3’s exist
but that seems overly recursive

If you meant you would only re-color the triplets you find, that doesn’t seem onerous.

that was what I meant… but that might still create triplets depending on the random value…

Edit: never mind. Didn’t completely grasp what you were asking.

for those interested… here is what I came up with… and it is actually quite fast

`````` private func removeTriplets() {
var found : Int = 0
let clr = option_Game_Level + 3

for yy in(0...theGRIDY-1) {
for xx in(0...theGRIDX-1) {
let clr = enumCOLOR(rawValue:Int.random(in: 1..<clr))!
let x1=xx-1
let x2=xx+1
if x1>=0 && x2<theGRIDX {
if sgBoard[x1,yy]?.color==sgBoard[xx,yy]?.color && sgBoard[xx,yy]?.color==sgBoard[x2,yy]?.color {
sgBoard[xx,yy] = tileRecord(color:clr,selected:false,highlight:false)
found+=1
}
let y1=yy-1
let y2=yy+1
if y1>=0 && y2<theGRIDY {
if sgBoard[xx,y1]?.color==sgBoard[xx,yy]?.color && sgBoard[xx,yy]?.color==sgBoard[xx,y2]?.color {
sgBoard[xx,yy] = tileRecord(color:clr,selected:false,highlight:false)
found+=1
}

}
}
}
}
if found>0 { removeTriplets() }
return
}
``````