Thursday 9 April 2009

In Search of Processings Lost

As you may have noticed, Processing programs that use random() and/or noise() and do not set the seed for these functions before they are called, create a different output for each run. The run that gets the superb effect is lost forever but setting seeds always gets the same old effect as the last time. To retain random creation and the capability of recreating a successful run it would nice if the value of seeds could be read at the beginning of a run and stored for future reference. Unfortunately this is not possible - the underlying Java Random class sets seed randomly from its () constructor and does not include a method for retrieving a value that can be used with setSeed().

A workaround is to generate a value for seed using a similar procedure to Random's, record it, and then pass it to a seed setter:


void setup() {
size(WIDTH, HEIGHT);
long seed = 8682522807148013L + System.nanoTime();
System.out.println(seed);
randomSeed(seed);
doDrawing();
}

output: 8702777365341749

If this run turns out to be the great one change setup() to

void setup() {
size(WIDTH, HEIGHT);
// long seed = 8682522807148013L + System.nanoTime();
// System.out.println(seed);
randomSeed(8702777365341749L);
doDrawing();
}

identical graphic output is recreated. Otherwise, just continue with the original setup() version and hope for the best. The same seed generation method is appropriate for noiseSeed(). The seed calculation shown will avoid holes in the total sequence of values returned by nextLong() - if such exist and affect the 48 bit seed used by Random that is.

No comments:

Post a Comment