# Weird randomSeed() behaviour

**URL:** <https://discourse.processing.org/t/weird-randomseed-behaviour/44052>\
**Category:** Beginners\
**Created:** [March 8, 2024, 2:37pm UTC](https://discourse.processing.org/t/weird-randomseed-behaviour/44052 "2024-03-08T14:37:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ripolas](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@ripolas](https://discourse.processing.org/u/ripolas)\
**Post date:** [March 8, 2024, 2:37pm UTC](https://discourse.processing.org/t/weird-randomseed-behaviour/44052/1 "2024-03-08T14:37:38Z")

</div>

Hey, so I found this weird thing while experimenting with randomSeed();

```auto
void setup(){
  for(int i = -100;i<100;i++){
    randomSeed(i);
    println(i,int(random(100)));
  }
}

```

this code prints the i, then the first random number with a set seed, BUT the random number is 27 with negative seeds and 73 with positive and 0. Can anyone explain this?? or am i doing something wrong? Thank you in advance.

-Ripolas

---

<div class="post-metadata">

**Author:** ![ripolas](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@ripolas](https://discourse.processing.org/u/ripolas)\
**Post date:** [March 8, 2024, 2:43pm UTC](https://discourse.processing.org/t/weird-randomseed-behaviour/44052/2 "2024-03-08T14:43:33Z")

</div>

Also, some other observations:  
with 10 its the exact same - just the numbers are 2 and 7 (first digits of 27 and 73?!?!)  
then with 1000 the numbers vary a little, but stay around 270 and 730. same with bigger numbers. they vary a bit more, but never the less - WEIRD

---

<div class="post-metadata">

**Author:** ![TiborUdvari](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiborudvari/32/18018_2.png) [@TiborUdvari](https://discourse.processing.org/u/TiborUdvari)\
**Post date:** [March 8, 2024, 5:11pm UTC](https://discourse.processing.org/t/weird-randomseed-behaviour/44052/3 "2024-03-08T17:11:35Z")

</div>

The implementation is in this file [processing4/core/src/processing/core/PApplet.java at main · benfry/processing4 · GitHub](https://github.com/benfry/processing4/blob/main/core/src/processing/core/PApplet.java)

In processing the usual workflow is to set the seed once, maybe something to do with the underlying java implementation. In any case, I agree it’s unexpected behaviour [https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/java/util/Random.html](https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/java/util/Random.html)

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 8, 2024, 5:43pm UTC](https://discourse.processing.org/t/weird-randomseed-behaviour/44052/4 "2024-03-08T17:43:06Z")

</div>

Every time you use `randomSeed` you are resetting the seed value for the next sequence of random numbers. The seed value created for `randomSeed(-100)` is very close to `randomSeed(-99)` is very small (bitwise) so that the result from `int(random(100))` is also very small. Also your code masks the actual difference because you force the result to an integer try `random(100)` instead.

If you try

```auto
void setup(){
  for(int i = -100;i<100;i++){
    randomSeed(i*10000);
    println(i,random(100));
  }
}

```

You will see what I mean when you use large numbers to initialise the seed. It is common to do

`randomSeed(System.currentTimeMillis())`

to guarantee a random sequence everytime the program is run.

The difference between negative and positive numbers (zero is positive in your code) is also easy to explain. Negative integers set their most significant bit and the random seed and random numbers are generated by performing a complex bit manipulation algorithm to get the next result.

So nothing really surprising but an interesting diversion. 😄

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 8, 2024, 5:46pm UTC](https://discourse.processing.org/t/weird-randomseed-behaviour/44052/5 "2024-03-08T17:46:36Z")

</div>

> [@TiborUdvari](#):
>
> In processing the usual workflow is to set the seed once

@TiborUdvari is absolutely right here. The point of being able to set the random seed is to get a repeatable series of random numbers which can be used to debug code.
