dimanche 14 juin 2015

How random is arc4random (mac os x)? (or what am I doing wrong?)

I'm playing with an optimized game of life implementation in swift/mac_os_x. First step: randomize a big grid of cells (50% alive).

code:

for(var i=0;i<768;i++){
  for(var j=0;j<768;j++){
    let r = Int(arc4random_uniform(100))
    let alive = (aliveOdds > r)
    self.setState(alive,cell: Cell(tup:(i,j)),cells: aliveCells)
  }
}

I expect a relatively uniform randomness. What I get has definite patterns:

Large image

Zooming in a bit on the lower left:

zoomed in

(I've changed the color to black on every 32 row and column, to see if the patterns lined up with any power of 2).

Any clue what is causing the patterns? I've tried: - replacing arc4random with rand(). - adding arc4stir() before each arc4random_uniform call - shifting the display (to ensure the pattern is in the data, not a display glitch)

Ideas on next steps?




Aucun commentaire:

Enregistrer un commentaire