How do you get different random images from the `CIRandomGenerator` filter?

Viewed 480

It appears to me that the output of CIFilter(name: "CIRandomGenerator") is always the same, regardless of whether one references the outputImage of an instance multiple times or whether one instantiates a new filter:

import CoreImage
let randomFilter = CIFilter(name: "CIRandomGenerator")
let outputImage = randomFilter!.outputImage!.cropped(to: (CGRect(x: 0, y: 0, width: 100, height: 100)))
let outputImage2 = randomFilter!.outputImage!.cropped(to: (CGRect(x: 0, y: 0, width: 100, height: 100)))
// pause for some amount of time so that it's definitely not 
// instantiating a PRNG with the same timestamp
let rf2 = CIFilter(name: "CIRandomGenerator")
rf2!.outputImage!.cropped(to: (CGRect(x: 0, y: 0, width: 100, height: 100)))

Leads to: playground output

Which, if you examine the pixels, are all the same.

I do not see any kind of seed parameter that can be passed in to initialize the pseudo-random number generator properly.

Since the CIFilter has infinite extent, I can randomly offset the location in the call to cropped and that appears to work, but it seems like a hack. Am I missing the proper way to initialize / getNext this random filter?

1 Answers
Related