Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

core-image

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?

like image 880
Larry OBrien Avatar asked Aug 31 '25 06:08

Larry OBrien


1 Answers

I almost put this as a comment, but I feel it's answer worthy.

That actually seems like you've correctly described it to me. Consider - what is a random number generator? Sure it may take a "seed", but ultimately most generators will provide a fairly predictable number if ran time and time again. (Provided you don't start adding other things to the equation.)

So the output of CIRandomGenerator may be thought of as the same kind of thing. Taken from (0,0) it's "random" out to infinity. But once you've "seeded" things - given an origin other than (0,0) - you've effectively "captured" something most would consider "random".


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!