# The first random value is always the same when using randomSeed

**URL:** https://discourse.processing.org/t/the-first-random-value-is-always-the-same-when-using-randomseed/21700
**Category:** Beginners
**Created:** [June 9, 2020, 7:27am UTC](https://discourse.processing.org/t/the-first-random-value-is-always-the-same-when-using-randomseed/21700 "2020-06-09T07:27:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Reva](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/reva/32/9561_2.png) [@Reva](https://discourse.processing.org/u/Reva)
#### Post date: [June 9, 2020, 7:27am UTC](https://discourse.processing.org/t/the-first-random-value-is-always-the-same-when-using-randomseed/21700/1 "2020-06-09T07:27:53Z")

</div>

I’ve found that no matter how I change the seed, the first random value is always the same when using randomSeed(). Here is the a really simple test:

```auto
void draw() {
  randomSeed(frameCount);
  println(frameCount + ": " + (int)random(10) + " " + (int)random(10) + " " + (int)random(10) );
}

```

In this case, the first value is always 7, but the later two numbers seems random. I have to dispose the first value in my application. Did I do anything wrong? 🤔

---

<div class="post-metadata">

### Author: ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)
#### Post date: [June 9, 2020, 7:58am UTC](https://discourse.processing.org/t/the-first-random-value-is-always-the-same-when-using-randomseed/21700/2 "2020-06-09T07:58:45Z")

</div>

Hi Reva,

I might be wrong but, try initializing the randomSeed() only one time at setup:

```auto
void setup(){
  randomSeed(frameCount);
}

void draw() {
  println(frameCount + ": " + (int)random(10) + " " + (int)random(10) + " " + (int)random(10) );
}

```

and please have a look at [https://processing.org/reference/randomSeed\_.html](https://processing.org/reference/randomSeed_.html)  
If you " Set the **seed** parameter to a constant it will return the same pseudo-random numbers each time the software is run.". If you don’t which this to happen just don’t set the randomSeed()

I hope it helps! 🙂

---

<div class="post-metadata">

### Author: ![Reva](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/reva/32/9561_2.png) [@Reva](https://discourse.processing.org/u/Reva)
#### Post date: [June 9, 2020, 8:57am UTC](https://discourse.processing.org/t/the-first-random-value-is-always-the-same-when-using-randomseed/21700/4 "2020-06-09T08:57:15Z")

</div>

Thanks for your reply~  
In fact, I use randomSeed() somewhere else to generate some pattern, and the seed is determined according to the user’s input. Later I want to simulate arbitrary input to test the program, but I found once I use randomSeed(), I can’t reset it to the “random()” state. So I change the seed in runtime. Then I found this situation. Is there any way to switch between seed mode and default mode?

---

<div class="post-metadata">

### Author: ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)
#### Post date: [June 9, 2020, 12:44pm UTC](https://discourse.processing.org/t/the-first-random-value-is-always-the-same-when-using-randomseed/21700/5 "2020-06-09T12:44:43Z")

</div>

Hi Reva,

Not that I am aware, but check this link: [https://forum.processing.org/two/discussion/22852/random-numbers-after-using-randomseed](https://forum.processing.org/two/discussion/22852/random-numbers-after-using-randomseed)  
You can try using time as a new seed, or you can try create object’s each with a random process. Example, one object has in its contructor the randomSeed(), and another does not. Then, you create a getter to retrieve the random number. Not sure if it will work for your case, but give it a try.  
Later on, I will try it myself, but let me know if it worked.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [June 10, 2020, 5:43pm UTC](https://discourse.processing.org/t/the-first-random-value-is-always-the-same-when-using-randomseed/21700/6 "2020-06-10T17:43:05Z")

</div>

> [@Reva](#):
>
> the first value is always 7, but the later two numbers seems random.

Correlation of the first numbers is a well-documented aspect of the Java PRNG. For more, read:

> <https://stackoverflow.com/questions/12282628/why-are-initial-random-numbers-similar-when-using-similar-seeds>

(Aside: This [correlation also happens in p5.js](https://github.com/processing/p5.js/issues/3691).)

If you want a pseudo-random number, don’t set the random seed each frame – and _especially_ don’t set the seed with 1, 2, 3, 4, 5, et cetera, because contiguous first values are highly correlated. Instead, either:

1. set the seed once in setup `randomSeed(0)` and then call random() in draw as often as you want with no correlation, or
2. if you _must_ set a different seed each frame (which is a bad idea), at least do it from non-contiguous numbers, for example multiplying the frameCount by a large prime number: `randomSeed(frameCount*1777)`

( or don’t set the seed at all!)

---

<div class="post-metadata">

### Author: ![Reva](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/reva/32/9561_2.png) [@Reva](https://discourse.processing.org/u/Reva)
#### Post date: [June 12, 2020, 1:21pm UTC](https://discourse.processing.org/t/the-first-random-value-is-always-the-same-when-using-randomseed/21700/7 "2020-06-12T13:21:41Z")

</div>

Thanks a lot, finally know the reason. 😃
