How to get the random function to stop generating random numbers

edited February 2018 in Questions about Code

I'm having some difficulty with my project. It's not entirely done yet, but I have a good start on it. I want to generate a random color for each pixel of the background. (Eventually I'll want to make it less random, so that there's a black vignette around the corners and then variations of bluish green colors in the center). Right now, I have it as to where it will generate a random grayscale color for each pixel, which I found code to do that in the processing examples. Unfortunately, the random number generator will continue to keep generating random numbers for some reason which causes each pixel to continually change color values (I want my background to be static after it has generated a value for each pixel). Here's the code, there's some extra stuff in there, but I believe the code in question is in the test() function.

float x;
float y;
float[][] points;
float amplitude = 150.0;
int num_waves = 15;
int[][] grid;

void setup()
{
  size(640,640);
}

void draw()
{
//background(); 
test();

translate(0,height/2);

for(int j=0; j<num_waves; j++)
{
  strokeWeight(1);
  stroke(0);
  translate(-2,4);
  sineWave();
}
}

float[][] Points()
{

  points = new float[width*3][2];
  x = 0.0;
  for(int j=0; j<points.length; j++)
  {
    y = amplitude*pow(sin(.015*x),5)*cos(.015*x);
    for(int i=0; i<2; i++)
    {
      switch(i)
      {
        case 0:
        points[j][i]=x;
        break;
        case 1:
        points[j][i]=y;
        break;
      }
    }
    x+=PI/4;
  }
  return(points);
}


void sineWave()
{
  float[][] points = Points();
  beginShape();
  vertex(0,0);
  noFill();
  for(int n=0; n<points.length; n++)
  {
    vertex(points[n][0],points[n][1]);
  }

  endShape();
}

void background()
{
noStroke();
     int Block_x = 32;
int Block_y = 32;

int Block_width = width/Block_x;
int Block_height = height/Block_y;

int Pallet_Step = 256/(Block_x+Block_y);

    for(int j=0; j<=Block_y; j++)
    for(int i=0; i<=Block_x; i++)
    {
      fill(pow((i+j),.8)*Pallet_Step,(i*j)/Pallet_Step,Pallet_Step*(j+1)-i);
      rect(i*Block_width, j*Block_height, (i+1)*Block_width, (j+1)*Block_height);
    }
}



  void test()
{
  grid = new int[width][height];
for(int h=0; h< width; h++)
{
  for(int d=0; d< height; d++)
  {
    grid[h][d] = int(random(255));
  }
}

for (int t=0; t<width; t++)
{
  for (int e=0; e<height; e++)
  {
    stroke(grid[t][e]);
    point(t,e);
  } 
}
}

Answers

  • edited February 2018 Answer ✓

    Well, if you aren't using random anywhere else except your background then you can use randomSeed https://processing.org/reference/randomSeed_.html after line 91 to force the random sequence to always be the same

  • Works great thanks :) I am thinking about using random later in my code though, hopefully I can figure out what to do then

  • if you only want to generate a random background once then use a pgraphic, generate that in setup, and use it as an image in your background() call.

    (java's math.Random allows different random streams, each with their own seed, so you could use that, but the api is different to the processing version)

  • Unfortunately, the random number generator will continue to keep generating random numbers for some reason which causes each pixel to continually change color values (I want my background to be static after it has generated a value for each pixel).

    Drawing once to a PGraphics rather than changing random sounds like the way to go. This would also make it easier to cross-fade the fixed image of your static background into your vignette background, for example by using tint.

Sign In or Register to comment.