-2

This code passes 3 as an argument to the arc4random_uniform() function and I guess that it returns true or false. Next you assign an enumerator to a variable. I don't understand what the function does, though.

let randomState = arc4random_uniform(3) == 2 ? CellState.Alive :
                    CellState.Empty
let cell = Cell(grid: self,
                pos: (i, j),
                state: randomState)

I am not following that logic.

jscs
  • 63,694
  • 13
  • 151
  • 195
bensams127
  • 119
  • 1
  • 5
  • What exactly don't you understand? Or, better, what _do_ you understand? – jscs Oct 06 '16 at 18:28
  • My guess is that you pass 3 as an argument to a function and it returns true or false. Next you assign an enumerator to a variable. The only question is what the function does – pailhead Oct 06 '16 at 18:30
  • arc4random_uniform(3) returns randomly 0, 1 or 2. So it is saying that if the random number is equal to 2 (33.33% odds to happen) it will use CellState.Alive otherwise it is equal to 0 or 1 (66.66% odds to happen) it will be CellState.Empty – Leo Dabus Oct 06 '16 at 19:07

1 Answers1

0

Is it the "arc4random_uniform(3) == 2 ? CellState.Alive : CellState.Empty" format that you don't understand? It's written as a conditional (ternary) operator:

condition ? expr1 : expr2

Basically, if func arc4random_uniform(3) == 2, let randomState == CellState.Alive, otherwise let randomState == CellState.Empty.