Using slider to change randomSeed

apaapa
edited October 2015 in Library Questions

Hi all, i'm trying to control a randomSeed() parameter with a slider to create a random number that i can use again in a running sketch, i'm having problems to change the randomSeed, it always get stucked, if i comment the line 31( "sliderValue= int(random(10));") it runs ok, but of course i don't have the random, here's the sketch:

import controlP5.*;

ControlP5 cp5;

//int sliderValue;
int caos, sliderValue;
color myColor;

void setup() {
  size(700, 400);
  noStroke();
  cp5 = new ControlP5(this);

  cp5.addSlider("sliderValue")
    .setPosition(100, 50)
      .setRange(0, 10)
        .setNumberOfTickMarks(10)
          ;

  cp5.addSlider("caos")
    .setPosition(100, 100)
      .setRange(0, 10)
        .setNumberOfTickMarks(10)
          ;
}

void draw() {
  background(0);

  randomSeed(caos);
  sliderValue= int(random(10));

  fill(map(sliderValue, 0, 10, 0, 255));
  rect(0, 0, width, 100);

  println("SLIDER VALUE", sliderValue);
  println("CAOS", caos);
}

void slider(float theColor) {
  myColor = color(theColor);
  println("a slider event. setting background to "+theColor);
}

Answers

  • edited October 2015

    According to randomSeed()'s reference:
    https://Processing.org/reference/randomSeed_.html

    It resets random() back to a pre-determined sequence of pseudo-random values.
    You should execute randomSeed() when the slider's value is changed rather than each single draw().

  • apaapa
    edited October 2015

    Hi, thanks for your answer, i've tryied that, maybe i'm missing something, here's the code:

    import controlP5.*;
    ControlP5 cp5;
    
    int caos, sliderValue;
    color myColor;
    
    void setup() {
      size(700, 400);
      noStroke();
      cp5 = new ControlP5(this);
    
      cp5.addSlider("sliderValue")
        .setPosition(100, 50)
          .setRange(0, 10)
            .setNumberOfTickMarks(10)
              ;
    
      cp5.addSlider("caos")
        .setPosition(100, 100)
          .setRange(0, 10)
            .setNumberOfTickMarks(10)
              ;
    }
    
    void draw() {
      background(0);
    
      fill(map(sliderValue, 0, 10, 0, 255));
      println(sliderValue);
      rect(0, 0, width, 100);
    }
    
    public void caos(int theValue) {
      randomSeed(theValue);
      int r = int(random(100));
      sliderValue = r;
    
      println("CAOS", theValue);
      println("r", r);
    }
    
    public void sliderValue(int theValue) {
      sliderValue = theValue;
      println("SLIDER", theValue);
    }
    
  • I said you should only change randomSeed() when the "seed" actually changes.
    I didn't mention anything about not using random() itself! :-<

  • I thought the seed was only good to repeat a serie of pseudo-random each time the program runs. How this works during runtime?

    from reference:

    Sets the seed value for random(). By default, random() produces different results each time the program is run. Set the seed parameter to a constant to return the same pseudo-random numbers each time the software is run.

Sign In or Register to comment.