# randomSeed() reuse

**URL:** <https://discourse.processing.org/t/randomseed-reuse/32154>\
**Category:** Beginners\
**Created:** [September 8, 2021, 7:51am UTC](https://discourse.processing.org/t/randomseed-reuse/32154 "2021-09-08T07:51:31Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hermes](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@Hermes](https://discourse.processing.org/u/Hermes)\
**Post date:** [September 8, 2021, 7:51am UTC](https://discourse.processing.org/t/randomseed-reuse/32154/1 "2021-09-08T07:51:31Z")

</div>

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

```auto
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");
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [September 8, 2021, 8:47am UTC](https://discourse.processing.org/t/randomseed-reuse/32154/2 "2021-09-08T08:47:32Z")

</div>

Hi @Hermes,

Welcome to the forum! 😉

> [@Hermes](#):
>
> how to reuse the same seed to generate the same image

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

In this case, use [`frameCount`](https://processing.org/reference/frameCount.html) 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()`](https://processing.org/reference/random_.html) 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).

---

<div class="post-metadata">

**Author:** ![Hermes](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@Hermes](https://discourse.processing.org/u/Hermes)\
**Post date:** [September 8, 2021, 3:31pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/3 "2021-09-08T15:31:02Z")

</div>

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"

> **[Exporting High-Resolution Images in Processing](https://sighack.com/post/exporting-high-resolution-images-in-processing)**
>
> A tutorial on two simple ways to export high-resolution images of your artwork in Processing.

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

> **[Utilizing Random Number Generator Seeds — Tyler Hobbs](https://tylerxhobbs.com/essays/2016/utilizing-random-number-generator-seeds)**
>
> Most generative artwork heavily  utilizes randomness , making the output of each run unique. That's usually a good thing, but sometimes it's useful to be able to regenerate exactly the same image. For example, while developing a piece of...

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 🙂

---

<div class="post-metadata">

**Author:** ![BrendanL](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brendanl/32/14076_2.png) [@BrendanL](https://discourse.processing.org/u/BrendanL)\
**Post date:** [September 8, 2021, 4:55pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/4 "2021-09-08T16:55:39Z")

</div>

> [@Hermes](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![Hermes](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@Hermes](https://discourse.processing.org/u/Hermes)\
**Post date:** [September 8, 2021, 5:08pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/5 "2021-09-08T17:08:33Z")

</div>

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

 ![seedtest](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c1c9cde5712d952f96dbc3018f76de9abd8d3f16.png)

---

<div class="post-metadata">

**Author:** ![BrendanL](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brendanl/32/14076_2.png) [@BrendanL](https://discourse.processing.org/u/BrendanL)\
**Post date:** [September 8, 2021, 5:21pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/6 "2021-09-08T17:21:38Z")

</div>

[randomSeed](https://processing.org/reference/randomSeed_.html) 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`.

---

<div class="post-metadata">

**Author:** ![Hermes](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@Hermes](https://discourse.processing.org/u/Hermes)\
**Post date:** [September 8, 2021, 5:38pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/7 "2021-09-08T17:38:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BrendanL](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/brendanl/32/14076_2.png) [@BrendanL](https://discourse.processing.org/u/BrendanL)\
**Post date:** [September 8, 2021, 5:43pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/8 "2021-09-08T17:43:29Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Hermes](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@Hermes](https://discourse.processing.org/u/Hermes)\
**Post date:** [September 8, 2021, 5:52pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/9 "2021-09-08T17:52:52Z")

</div>

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();

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 8, 2021, 6:41pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/10 "2021-09-08T18:41:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hermes](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@Hermes](https://discourse.processing.org/u/Hermes)\
**Post date:** [September 8, 2021, 6:48pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/11 "2021-09-08T18:48:36Z")

</div>

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

> **[Utilizing Random Number Generator Seeds — Tyler Hobbs](https://tylerxhobbs.com/essays/2016/utilizing-random-number-generator-seeds)**
>
> Most generative artwork heavily  utilizes randomness , making the output of each run unique. That's usually a good thing, but sometimes it's useful to be able to regenerate exactly the same image. For example, while developing a piece of...

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 8, 2021, 8:28pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/12 "2021-09-08T20:28:39Z")

</div>

I really don’t understand your issue.

Hope this help:

```auto
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));
  }
}

```

---

<div class="post-metadata">

**Author:** ![Hermes](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@Hermes](https://discourse.processing.org/u/Hermes)\
**Post date:** [September 8, 2021, 8:37pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/13 "2021-09-08T20:37:28Z")

</div>

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 … ?

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 9, 2021, 4:28pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/14 "2021-09-09T16:28:27Z")

</div>

I still don’t get your question.

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

```auto
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:

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![Hermes](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@Hermes](https://discourse.processing.org/u/Hermes)\
**Post date:** [September 9, 2021, 4:35pm UTC](https://discourse.processing.org/t/randomseed-reuse/32154/15 "2021-09-09T16:35:10Z")

</div>

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

```auto
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");
}

```

---

<div class="post-metadata">

**Author:** ![Hermes](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@Hermes](https://discourse.processing.org/u/Hermes)\
**Post date:** [September 26, 2021, 8:36am UTC](https://discourse.processing.org/t/randomseed-reuse/32154/16 "2021-09-26T08:36:03Z")

</div>

also this works nicely

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

```
