Adding a random value to each item in array?

Hello, I am trying to achieve a loop that runs through an array with 120 values for sin and a second array that runs through 120 values for cos. Every time the loop runs I want to add a random number to the previous value.

Each loop should increase each value in an array with a number (for example) between 1 and 3.

This process should repeat for 125 times. So both complete arrays need to be iterated 125 times.

These are the arrays:

  float[] cos_vals = new float[degrees];
  float[] sin_vals = new float[degrees];

And these the loops I use to iterate through the process:

for (float r=0; r<dia; r+=stepSize)
    {
    for (int i=0; i < degrees; i++) {         
      cos_vals[i] = cos(TWO_PI/degrees * i);
      sin_vals[i] = sin(TWO_PI/degrees * i);
    }
  }

What I would normally do is add a value at the end of the first loop that stores the previous value like:

latestA = currentA;

And then use ‘currentA’ in the loop again. But how would I go about doing this in an array? Can I somehow add a random value to each item in the array and then say:

latestArray = currentArray;

And how would I do that?

I hope my question is clear :slight_smile: if not, let me know!

Your logic is getting out of hand. You have several arrays with values, and are looping over them, changing them, and now you need more arrays to hold previous values! It’s getting complicated.

You need to simplify your code, and the right way to do that here is to use a class that contains all the data (and possibly a few functions to work with that data) that you need for one thing. And then have a single array of those things.

Consider:

class DataContainer {
  float cos_val, sin_val, prev_cos, prev_sin;
  DataContainer(){
  }
  void update(float new_cos, float new_sin){
    prev_cos = cos_val;
    prev_sin = sin_val;
    cos_val = new_cos % TWO_PI;
    sin_val = new_sin % TWO_PI;
  }
}

// ---

DataContainer[] data = new DataContainer[degrees];

// ---

for( int i = 0; i < data.length; i++){
  data[i].update( value, value )
}

(Not running code. Look at what it does.)

2 Likes

Hello, thanks for taking the time. I have to admit, I am trying really hard but it doesn’t click in my brain. I have written a lot of scripts but when scripts start to be efficient I lose track of what goes where. It becomes abstract.

I have tried to make your script work, but don’t understand what to place in the area of:
‘( value, value )’ I thought that I could get away with testing by simply replacing the values with a 1, but it gives a NullPointerException.

Also, I have to pull the data from somewhere. I’m afraid I don’t understand how it works. I do manage to make simple arrays, but I guess I’m on a lifelong quest to make something click in my mind, it just doesn’t seem to want to do it. Maybe I’m getting early onset dementia, I don’t know, I’m starting to get worried. I literally finished the whole book of ‘Nature of Coding’.

I’ll share my entire code below of what I have now, in relation to what I’m attempting, maybe I gave too little information on what the purpose is of this code, it’s for an artwork so I was trying to only show the relevant bits and people have been trying to recreate some of my drawings so I was a bit protective of my ‘idea’. But in order to learn I have to expose my idea I guess.

import processing.svg.*;
boolean record;

void setup() {
  size(1000, 1000);
  background(255);
  noLoop();
  stroke(1);
  noFill();
  //save to file with same name as the sketch + timestamp
  //beginRecord(SVG, getClass().getName()+day()+month()+year()+hour()+minute()+second()+".svg");
}

//float degrees = 360;
float counts = 3;
float rings = 125;
float dia = 900;
float radius = dia/2;
float stepSize = dia/rings;

void draw() {
    translate(width/2, height/2);

  int degrees = 120;
  float[] cos_vals = new float[degrees];
  float[] sin_vals = new float[degrees];

  // Use a for() loop to quickly iterate
  // through all values in an array.
  for (float r=0; r<dia; r+=stepSize) // draw rings from the center on outwards, fitting $rings in $dia
    {
    noFill();
    beginShape();
    for (int i=0; i < degrees; i++) {         
      cos_vals[i] = cos(TWO_PI/degrees * i);
      sin_vals[i] = sin(TWO_PI/degrees * i);
      vertex((r/2)*sin_vals[i], (r/2)*cos_vals[i]); // draw the circles
      //println(cos_vals[i]);
    }
    endShape(CLOSE);
  }
//endRecord();
}
import processing.svg.*;
boolean record;

class DataContainer {
  float cos_val, sin_val, prev_cos, prev_sin;
  DataContainer(float new_cos, float new_sin) {
    cos_val = new_cos;
    sin_val = new_sin;
    prev_cos = cos_val;
    prev_sin = sin_val;
  }
  void update(float new_cos, float new_sin) {
    prev_cos = cos_val;
    prev_sin = sin_val;
    cos_val += random(-0.0001,0.0001);
    sin_val += random(-0.0001,0.0001);
  }
}

DataContainer[] data = new DataContainer[120];

void setup() {
  size(1000, 1000);
  background(255);
  stroke(1);
  noFill();

  for (int i=0; i < data.length; i++) {
    data[i] = new DataContainer( cos(TWO_PI/120.0 * i), sin(TWO_PI/120.0 * i) );
  }

  //save to file with same name as the sketch + timestamp
  //beginRecord(SVG, getClass().getName()+day()+month()+year()+hour()+minute()+second()+".svg");
}

//float degrees = 360;
float counts = 3;
float rings = 12;
float dia = 900;
float radius = dia/2;
float stepSize = dia/rings;

void draw() {
  translate(width/2, height/2);
  // Use a for() loop to quickly iterate
  // through all values in an array.
  for (float r=0; r<dia; r+=stepSize) {// draw rings from the center on outwards, fitting $rings in $dia
    beginShape();
    for (int i=0; i < data.length; i++) {         
      data[i].update( cos(TWO_PI/120.0 * i), sin(TWO_PI/120.0 * i) );
      vertex((r/2)*data[i].sin_val, (r/2)*data[i].cos_val); // draw the circles
      //println(cos_vals[i]);
    }
    endShape(CLOSE);
  }
  //endRecord();
}

i CUT DOWN THE NUMBER OF RINGS SO YOU CAN SEE THE SMALL CHANGES IN THE POSITIONS.

1 Like