randomSeed() reuse

hello I’m a noob to Processing, I’ve figured out how to generate a seed for each image output but I can’t figure out how to reuse the same seed to generate the same image… I just need to know the format and where to put it, yes I searched in examples and in the forums … and have tried many things … thx in advance

float seed = System.nanoTime();

void setup(){

  colorMode(HSB);
  size(1000, 1000);
  background(360, 100, 100);
}

 void draw() {
 
// code here
}
                                                          
void keyPressed() {
 
  if (key == 's') {     
    saveImage();
  } 
}

void saveImage() {

  save("name"+"-"+ seed +"-.png");
  println("Saved");
}

Hi @Hermes,

Welcome to the forum! :wink:

What do you mean by “same image”? Do you mean the same frame?

In this case, use frameCount which is the number of frames since the beginning of the sketch.

The system variable frameCount contains the number of frames displayed since the program started. Inside setup() the value is 0 and and after the first iteration of draw it is 1, etc.

If you just want a random number every time, just use the random() function:

Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. If only one parameter is passed to the function, it will return a float between zero and the value of the high parameter. For example, random(5) returns values between 0 and 5 (starting at zero, and up to, but not including, 5).

hi thanks I think I’m explaining this wrong of course here is a page that sort of explains what I’m referring to … it’s the 1st bit of code under " Creating Deterministic Sketches"

also Tyler Hobbs refers to this what I’m trying to do

basically I have the seed for any specific sketch but I can’t figure out the code to reuse the generated seed for a sketch I want to reproduce

thx :slight_smile:

You will need to save the seed somewhere and then call randomSeed(seed) inside of setup() to reuse it. Perhaps something like asking the user for the seed at run time so they can enter the one to generate the image they want.

2 Likes

yes that’s why I’m here I’m clueless

randomSeed takes an integer, not a float for one. I don’t see a syntax error in that image but I also can’t see the entire definition of setup.

1 Like

when System.nanoTime() saves the seed it’s a number like in the image above

What does the error message in that image say?

Your error is on line 6. 1.13063726E15 is not an integer. Use an integer value for the seed, something like 11306.

yes I figured that but … when I use float seed = System.nanoTime()
the seed is not an integer, I removed the 1st number and decimal
that works but it’s not the original image … I’m going to try millis();

If you set your seed to be millis() or System.nanoTime(), it will always be different on every run of the program. It is not because the program is the same and the line setting the seed at the same position that the same time will be elapsed to get to that line. It will depend on a lot of different parameters (the charge of your CPU, the temperature, …). And since the seed will always be different, the rest of the execution will also be.

1 Like

what I’m trying to do is use a seed … (this part I’ve figured out)
to regenerate the same sketch/image again … explained in the article

I know it’s going to be something really basic
the problem I’m getting also is with

float seed = System.nanoTime();
float seed = System.currentTimeMillis();

is that … randonSeed(); expects an integer

I really don’t understand your issue.

Hope this help:

long seed;

void setup() {
  seed = System.nanoTime(); // Get a random seed (will "always" be different for each execution)
  randomSeed(seed);         // Set the seed of the random number generator
  
  // Print a sequence of random numbers 
  // They will always be different from one execution to another
  for (int i = 0; i < 5; i++) {
    println(random(1));
  }

  println("");

  randomSeed(5); // Set the seed of the random number generator to 5 (It will always be 5)
  
  // Print a sequence of random numbers 
  // They will always be the same from one execution to another since the seed was set to 5
  for (int i = 0; i < 5; i++) {
    println(random(1));
  }
}
1 Like

thx … I will test this … if I have a seed from another image … seed = System.nanoTime() … I use this …
and I want to regenerate the same image/sketch … how would I plug that into your code … ?

I still don’t get your question.

What you would do for example is run a program like this:

long seed;

void setup() {
  seed = System.nanoTime(); // Get a random seed (will "always" be different for each execution)
  randomSeed(seed);         // Set the seed of the random number generator
  println(seed);            // Print out the seed used on screen
  
  // Do whatever you want to do to generate your image
 generateImage();
}

If there is an image for which you are happy with, then you can copy the seed written in the console.
Let’s say I run the code for the first time. The seed printed in the console is: 123456. But I don’t like the outcome so I run the program again.
This time the seed is: 987654 and I like the outcome. Then I can save that seed number.
If later on I want to regenerate the same image I will alter the previous code like this:

long seed;

void setup() {
  // seed = System.nanoTime(); // No need for this anymore
  seed = 987654;            // Set the seed to the value that generated the image I liked.
  randomSeed(seed);         // Set the seed of the random number generator
  println(seed);            // Print out the seed used on screen
  
  // Do whatever you want to do to generate your image
 generateImage();
}
2 Likes

yes thank you very much … I’ve done just about what you posted above…
here’s a working example using color bars just to test

set to “0L” for new sketch/image/seed,
or regenerate same image using seed from “System.nanoTime()” found in console, followed with “L”
seed will also be saved in the image filename

long seed = 1256239238541900L; //set to 0 for new sketch/image/seed, or regenerate  same image using seed from "System.nanoTime()" found in console, followed with "L" 
int x = 0;

void setup()

{
  if (seed == 0)
  {
    seed = System.nanoTime();
  }

  size(500, 400);
  colorMode(HSB, 100);
  noStroke();

  randomSeed(seed); //
  println(seed);
}

void draw()

{
    while (x < width) {
    int wi = int(random(100));
    fill(random(100), 80, 80);
    rect(x, 0, wi, height);
    x = x + wi;
  }
}

void keyPressed() {
  if (key == 's') {
    saveImage();
  }
}

void saveImage() {
  saveFrame("seed"+"-"+ seed + "-###.png");
  println("Saved");
}
1 Like

also this works nicely

noiseSeed(seed); // in the place of 
randomSeed(seed);