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

897 views Asked by At

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?

1

There are 1 answers

0
user1232690 On
  1. You cannot hit period or that many regular non-uniform clusters of arc4random on any displayable set (16*(2**31) - 1).

These are definitely signs of the corrupted/unininitialized memory. For example, you are initializing 768x768 field, but you are showing us 1024xsomething field.

  1. Try replacing Int(arc4random_uniform(100)) with just 100 to see.