# Random() seems to be very biased towards positive numbers. how to mitigate?

**URL:** <https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749>\
**Category:** Coding Questions\
**Created:** [November 27, 2020, 6:57am UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749 "2020-11-27T06:57:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![exe](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@exe](https://discourse.processing.org/u/exe)\
**Post date:** [November 27, 2020, 6:57am UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/1 "2020-11-27T06:57:55Z")

</div>

My level of expertise is pretty low, I have some mild background in Java and QBasic way back when, and otherwise have learned most of the little I know via Daniel’s YT tuts (of which I’ve still many to go through).

But my main interest is playing around with random number generation and noise. I’ve noticed repeatedly that when I call for a random value in a manner such as “random(-5, 5)” for example, that the results tend to be very skewed towards the positive end of that range. Right now, I’m basically working with a simple randomly moving point atop a faint graph, and with the applied force that moves the point drawing from that pool of random values on each loop, the point invariably is in the bottom right quadrant (positive x value, positive y value) the vast majority of the time, even though in theory it should be pretty much evenly distributed across the quadrants. I find that to get something that feels more evenly distributed, I have to go with a range along the lines of (-7,5). Going to (-10, 5) it starts to skew too heavily into negatives, but anything less than about -7, positives will be disproportionately favored, it seems.

Any guidance on why this is or how to mitigate it? I know more or less how to use the “noise” function, but that’s not what I’m after here.

Thanks for anyone who can provide some clarity to this issue.

---

<div class="post-metadata">

**Author:** ![exe](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@exe](https://discourse.processing.org/u/exe)\
**Post date:** [November 27, 2020, 7:11am UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/2 "2020-11-27T07:11:03Z")

</div>

Ahh, 5 minutes after posting, I figure it out…

I tested the random function free of any other context, and got a much more even-seeming distribution of numbers. So checking my code more carefully, I realized the problem must stem from a bit I had added wherein on each loop, the random value (‘fsx’, and ‘fsy’) get divided by 2. Then I had added a modifier wherein once the value of either became less than .01, it reset to a new random # between -5 and 5.

Problem of course being that the negative values are always less than .01, creating a massive imbalance in how the code executes. Changing that bit of code to “abs(fsx)” fixed the issue.

I do feel like it still leans positive just a bit though, and part of the reason for the initial post was because it’s a tendency I’ve noticed repeatedly. But maybe in each case there was just a contextual factor contributing to that, as was the case here.

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [November 27, 2020, 1:51pm UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/3 "2020-11-27T13:51:23Z")

</div>

try this code

```auto
void setup() {
  int zero=0, positive=0, negative=0;
  final int totalTest = 1000000;

  randomSeed(mouseX*mouseY);
  for (int i=0; i < totalTest; i++) {
    float r = random(-100, 100);
    if (r == 0.0) zero++;
    else if (r > 0) positive++;
    else negative++;
  }
  println("postive : ", positive, " (", ((float) positive) / totalTest, "%)");
  println("zero : ", zero, " (", ((float) zero) / totalTest, "%)");
  println("negative : ", negative, " (", ((float) negative) / totalTest, "%)");
}

void draw() {}

```

I get in the console:

postive : 500789 ( 0.500789%)  
zero : 0 ( 0.0%)  
negative : 499211 ( 0.499211%)

random() returns a float so The fact that 0.0 was never hit in one million draws is not surprising.

random() does not include the top value, so it’s not a totally fair interval. you have a bit more values below 0 than above zero but probably hard to notice

bumping totalTest to 1 000 000 000 (one billion tests) gave me

postive : 500009267 ( 0.5000093%)  
zero : 59 ( 5.9E-8%)  
negative : 499990674 ( 0.4999907%)

so this time I got a few (59) hits on 0.0 (at float precision)

You can see that this is fairly balanced between positive and negative in both cases anyway

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 28, 2020, 4:19pm UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/4 "2020-11-28T16:19:51Z")

</div>

> [@exe](#):
>
> I do feel like it still leans positive just a bit though,

This old random walker sketch leaned positive so much I had to use some arbitrary value to subtract on the values returned from **noise()**: 🐛  
[1D array affecting 2D array - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/6838/1d-array-affecting-2d-array#Item_4)

```auto
  void move() {
    x += SPD * (noise(xoff += inc) - NOISE_OFFSET);
    y += SPD * (noise(yoff += inc) - NOISE_OFFSET);

```

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [November 28, 2020, 4:28pm UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/5 "2020-11-28T16:28:28Z")

</div>

may be… 500009267 positives versus 499990674 means it has been ~10,000 (9 296.5) times positive when it could have been negative for a correct balance on 1 billion draws --\> that’s an error of less than 0.001% … not that bad.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 28, 2020, 4:30pm UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/6 "2020-11-28T16:30:56Z")

</div>

> [@jay\_m](#):
>
> That’s an error of less than 0.001%…

static final float NOISE\_OFFSET = JAVA? .4538 : .467;

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [November 28, 2020, 4:32pm UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/7 "2020-11-28T16:32:32Z")

</div>

we were talking random()

for the noise() function, the Perlin algorithm resulting value will always be between 0.0 and 1.0 - so it has to be stretched to your interval. As the doc states

> There have been debates over the accuracy of the implementation of noise in Processing. For clarification, it’s an implementation of “classic Perlin noise” from 1983, and not the newer “simplex noise” method from 2001

---

<div class="post-metadata">

**Author:** ![exe](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@exe](https://discourse.processing.org/u/exe)\
**Post date:** [November 28, 2020, 7:46pm UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/8 "2020-11-28T19:46:59Z")

</div>

Thanks for the information, Jay. I get the same result with your code. In fact, I get _exactly_ the same result. I’m likely misunderstanding something, but shouldn’t the result be a little different each time the program is run?

---

<div class="post-metadata">

**Author:** ![exe](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@exe](https://discourse.processing.org/u/exe)\
**Post date:** [November 28, 2020, 7:49pm UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/9 "2020-11-28T19:49:46Z")

</div>

As a side note, I had no idea you could abbreviate if/else if/else statements to the degree you do in that code. I thought you always needed curly-brackets.

---

<div class="post-metadata">

**Author:** ![exe](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@exe](https://discourse.processing.org/u/exe)\
**Post date:** [November 28, 2020, 7:51pm UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/10 "2020-11-28T19:51:35Z")

</div>

Ah … it’s because of “randomSeed” that it’s the same result exactly, is that correct? I’m still wrapping my head around seeds and how they apply/work with noise/random. [Edit: just tested this w/ randomSeed commented out to confirm my own suspicions. So much to learn!]

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [November 29, 2020, 12:19am UTC](https://discourse.processing.org/t/random-seems-to-be-very-biased-towards-positive-numbers-how-to-mitigate/25749/11 "2020-11-29T00:19:09Z")

</div>

If the `true` or `false` clause of an `if` statement has **only one statement** then you do not need to use braces (aka “curly brackets”).

This is considered dangerous though as it can lead to errors if you add more statements and do no realize the braces were not there, so most programming style guides recommend against doing so.

Yes the randomSeed thingy is not doing what I wanted with mouseX and Y to generate some randomness in the seed as it’s likely the mouse is not over the window in the setup and so you always get 0
