# Negative Noise problem

**URL:** https://discourse.processing.org/t/negative-noise-problem/2297
**Category:** Coding Questions
**Created:** [August 2, 2018, 9:37am UTC](https://discourse.processing.org/t/negative-noise-problem/2297 "2018-08-02T09:37:54Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)
#### Post date: [August 2, 2018, 9:37am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/1 "2018-08-02T09:37:54Z")

</div>

I found that when you put negative numbers into the noise function like xoff or yoff the noise function converts them to positive numbers. Why?? And how do I solve this to be able to put negative numbers into the noise function. Any help is appreciated.

Code is below ↓↓↓

```auto
float NoiseJump = 0.01;
int XPosition = -10;
int YPosition = -10;
int LengthToRun = 32;
int PositionInSave = 1;
String[] Save = new String[LengthToRun * LengthToRun + 1];
int NoiseScaleUpValue = 255;

float xoff = NoiseJump * (XPosition * LengthToRun);

  Save[0] = str(round(random(MAX_INT)));
  saveStrings("Numbers.txt", Save);
  for (int Row = 0; Row < LengthToRun; Row++) {
    xoff += NoiseJump;
    float yoff = NoiseJump * (YPosition * LengthToRun);
    for (int Colmn = 0; Colmn < LengthToRun; Colmn++) {
      yoff += NoiseJump;
      noiseDetail(12, 0.5);
      noiseSeed(33);
      float OceanNoiseGen = noise(xoff, yoff) * NoiseScaleUpValue;

      Save[PositionInSave] = "Row: " + Row + ", Colmn: " + Colmn + ", Value: " + OceanNoiseGen;
      PositionInSave++;

    }
  }
  
  saveStrings("Numbers.txt", Save);
  background(0, 255, 0);  

```

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [August 2, 2018, 9:52am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/2 "2018-08-02T09:52:01Z")

</div>

If you look at the documentation you’ll notice that the output of `noise()` should always be between 0 and 1.

[https://processing.org/reference/noise\_.html](https://processing.org/reference/noise_.html)

---

<div class="post-metadata">

### Author: ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)
#### Post date: [August 2, 2018, 10:17am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/3 "2018-08-02T10:17:26Z")

</div>

Yeah I am pretty sure that’s not the problem because i just scale it up by 255. I think the problem is the fact that if xoff = -0.1 and then goes into the noise function it will change xoff to 0.1 that’s what i can not understand. Why it does that and how to make it stay negative.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [August 2, 2018, 10:51am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/4 "2018-08-02T10:51:31Z")

</div>

It’s not clear from your message whether it’s the fact that the result is not negative that’s the problem, or the fact that it mirrors around zero (so the result of 0.1 is the same as -0.1)? If the latter, then you could add an offset to your input values so they’re always positive?

I’m not sure whether the mirroring around zero is expected behaviour, or just an optimization because the values are used to read from an array.

---

<div class="post-metadata">

### Author: ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)
#### Post date: [August 2, 2018, 11:56am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/5 "2018-08-02T11:56:43Z")

</div>

Thanks for your reply it helped a lot on realizing that noise cannot go into negatives because it only goes 0 to 1. Do you think to solve this problem I would need a to make it were noise goes from -1 to 1. Do you think this would work or would I have to make my own type of noise. Any thought or idea on this?

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [August 2, 2018, 12:49pm UTC](https://discourse.processing.org/t/negative-noise-problem/2297/6 "2018-08-02T12:49:42Z")

</div>

Can’t you just scale the output of noise?

eg.

```auto
float OceanNoiseGen = map(noise(xoff, yoff), 0, 1, -NoiseScaleUpValue, NoiseScaleUpValue);

```

---

<div class="post-metadata">

### Author: ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)
#### Post date: [August 2, 2018, 4:11pm UTC](https://discourse.processing.org/t/negative-noise-problem/2297/7 "2018-08-02T16:11:53Z")

</div>

I tried this but it still gives positive numbers.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [August 2, 2018, 4:44pm UTC](https://discourse.processing.org/t/negative-noise-problem/2297/8 "2018-08-02T16:44:42Z")

</div>

Yes, because the area of noise you’re looking at only has values in that region! If you change values like NoiseJump or LengthToRun you’ll see negative values. Let’s take a step back. What are you actually trying to do?

---

<div class="post-metadata">

### Author: ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)
#### Post date: [August 2, 2018, 5:24pm UTC](https://discourse.processing.org/t/negative-noise-problem/2297/9 "2018-08-02T17:24:43Z")

</div>

I am trying to make a noise map but the problem I am having is were when you move into the negative values it copies the same chunk but in positive numbers. Aka it changes any negative xoff or yoff to positive numbers this is the problem I am trying to solve.

When you change LengthToRun or NoiseJump in your sketch do they go negative. Because in mine I put a minus in front of them and it didn’t change anything?

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [August 2, 2018, 6:09pm UTC](https://discourse.processing.org/t/negative-noise-problem/2297/10 "2018-08-02T18:09:17Z")

</div>

Why would you make them negative? If you make them bigger you get negative _outputs_ with the map code I posted earlier.

If your problem is mirroring when you pass in negative _inputs_ then add a large enough offset to both numbers so they’re always positive, as I said earlier.

Other than those suggestions I really can’t help further because if that doesn’t cover it I don’t understand what you think is a problem.

---

<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: [August 3, 2018, 6:46am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/11 "2018-08-03T06:46:14Z")

</div>

Can you try to make a smaller example to demonstrate what your problem is please?

Something really simple and visual to explain what is the behavior that you try to avoid.

For what I understand the answer @neilcsmith gave you should work:  
`noise(xoff + xOffset, yoff + yOffset)`  
Where xOffset and yOffset are big enough constants.

---

<div class="post-metadata">

### Author: ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)
#### Post date: [August 3, 2018, 8:17am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/12 "2018-08-03T08:17:55Z")

</div>

Yeah I think that will work. Here’s a code demonstration of the error. Like I said before the error is that the negative numbers change into positive.

key q = blue mountains move left  
key a = blue mountains move right

key e = green mountains move left  
key d = green mountains move right

```auto
float noiseScale = 0.05;
float MinusNoiseX1 = -160;
float MinusNoiseX2 = -160;
float TileSize = 5;
float NoiseJumpMap = 255;
boolean[] Keys = new boolean[4];
float[] NoiseValues;

void setup() {
  fullScreen(P2D);
}

void draw() {
  background(52, 250, 215);
  
  KeysCommand();
  
  noiseDetail(48, 0.50);
  for (int x = 0; x < width / TileSize; x++) {
    
    float noiseVal = noise((x + MinusNoiseX1) * noiseScale, noiseScale);
    
    color Fill = lerpColor(color(noiseVal * NoiseJumpMap), color(0, 0, 100), 0.50);
    fill(Fill);
    stroke(Fill);
    rect(x * TileSize, noiseVal * NoiseJumpMap + 200, TileSize, height - noiseVal * NoiseJumpMap + 200);
  }
  //noiseDetail(24, 0.50);
  for (int x1 = 0; x1 < width / TileSize; x1++) {
    
    float noiseVal = noise((x1 + MinusNoiseX2) * noiseScale, noiseScale);
    
    color Fill = lerpColor(color(noiseVal * NoiseJumpMap), color(0, 255, 0), 0.50);
    fill(Fill);
    stroke(Fill);
    rect(x1 * TileSize, noiseVal * NoiseJumpMap + 350, TileSize, height - noiseVal * NoiseJumpMap + 350);
  }
  fill(255, 0, 0);
  text(MinusNoiseX1, 50, 50);
  fill(50, 70, 200);
  text(MinusNoiseX2, 50, 100);
  fill(0);
  strokeWeight(5);
  stroke(255);
  line(width/2, 0, width/2, height);
  fill(0);
  textSize(30);
  text("0", width/2, 50);
  text("Positive Numbers", width/1.5, 50);
  text("Negative Numbers", width/4, 50);
}

void keyPressed() {
  switch (key) {   
      case 'a' : 
        Keys[0] = true;
        break;
      case 'd' : 
        Keys[1] = true;
        break;
      case 'q' : 
        Keys[2] = true;
        break;
      case 'e' : 
        Keys[3] = true;
        break;
  }
}

void keyReleased() {
  switch (key) {   
      case 'a' : 
        Keys[0] = false;
        break;
      case 'd' : 
        Keys[1] = false;
        break;
      case 'q' : 
        Keys[2] = false;
        break;
      case 'e' : 
        Keys[3] = false;
        break;
  }
}

void KeysCommand() {
  float MovementSpeed = 100;
  
  if (Keys[0] == true) {
    MinusNoiseX1 -= noiseScale * MovementSpeed;
  }
  if (Keys[1] == true) {
    MinusNoiseX1 += noiseScale * MovementSpeed;
  }
  if (Keys[2] == true) {
    MinusNoiseX2 -= noiseScale * MovementSpeed;
  }
  if (Keys[3] == true) {
    MinusNoiseX2 += noiseScale * MovementSpeed;
  }
}

```

---

<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: [August 3, 2018, 8:28am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/13 "2018-08-03T08:28:18Z")

</div>

Ok so here what I got:

 ![NoisePb](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/a2a1c0153ed894d35a26cf5df02290afaf4e689a.jpg)

What’s the problem with that?

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [August 3, 2018, 8:46am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/14 "2018-08-03T08:46:24Z")

</div>

> [@imaginativeCODE](#):
>
> Like I said before the error is that the negative numbers change into positive.

What error?

It sounds like you’re not understanding how this function works. Can you offer a few input values, and where the output value isn’t what you expect, highlighting where _you_ think there’s a problem?

---

<div class="post-metadata">

### Author: ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)
#### Post date: [August 3, 2018, 10:54am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/15 "2018-08-03T10:54:49Z")

</div>

That’s weird did you change something in the code before you took the picture because here’s what I got.

 ![ErrorNoise](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/f72fa7c9b1372ce5f3a218f0a075fadf5890cb5e.jpg)

As you can see on the left is a copy of the right but backwards.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [August 3, 2018, 12:24pm UTC](https://discourse.processing.org/t/negative-noise-problem/2297/16 "2018-08-03T12:24:05Z")

</div>

> [@imaginativeCODE](#):
>
> As you can see on the left is a copy of the right but backwards.

Yes, it mirrors around zero. I assume @jb4x has altered the code as has been suggested multiple times - add a constant offset so the inputs to noise() are always positive.

---

<div class="post-metadata">

### Author: ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)
#### Post date: [August 3, 2018, 1:21pm UTC](https://discourse.processing.org/t/negative-noise-problem/2297/17 "2018-08-03T13:21:03Z")

</div>

Yeah I think so thanks for your help guys this will work.

---

<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: [August 3, 2018, 1:59pm UTC](https://discourse.processing.org/t/negative-noise-problem/2297/18 "2018-08-03T13:59:56Z")

</div>

That’s weird… I copy paste it and run it. That’s all.  
I just changed the displayed value the same way you did when you edited your post.

---

<div class="post-metadata">

### Author: ![imaginativeCODE](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/imaginativecode/32/4220_2.png) [@imaginativeCODE](https://discourse.processing.org/u/imaginativeCODE)
#### Post date: [August 4, 2018, 7:26am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/19 "2018-08-04T07:26:43Z")

</div>

Huh you must have plused a number into “MinusNoiseX1” to have stop the error.

---

<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: [August 4, 2018, 7:33am UTC](https://discourse.processing.org/t/negative-noise-problem/2297/20 "2018-08-04T07:33:18Z")

</div>

I just tried it again and I don’t have your problem…  
Which version of processing are you using?

[Next page](https://discourse.processing.org/t/negative-noise-problem/2297.md?page=2)
