变量使用”random()”每次调用时都会改变。

huangapple go评论51阅读模式
英文:

Variables using "random()" change everytime they are called

问题

I am drawing a colored object with random RGB color values via the background() function in Processing, waiting half a second, and then changing the position of the RGB values (e.g. swapping R with B, etc):

void draw() {
    z = float(random(255));
    x = float(random(255));
    c = float(random(255));
    background(z, x, c);
    delay(500);
    background(c, x, z);
}

However, the way I am declaring the color values randomly doesn't seem to be constant within the function, despite storing the values as variables; rather, the second background call is generating entirely new values for the RGB variables.

I want the z, x, and c variables to not change upon being called into the 2nd background() command, but can't quite figure it out.

I tried making other variables, using frameRate(1), and moving the delay(500) command past the 2nd background(), but unfortunately the variables change upon being called the 2nd time, and I want them to keep their value until the next void draw() loop.

英文:

I am drawing a colored object with random RGB color values via the background() function in Processing, waiting half a second, and then changing the position of the RGB values (e.g. swapping R with B, etc):

void draw() {
    z = float(random(255));
    x = float(random(255));
    c = float(random(255));
    background(z, x, c);
    delay(500);
    background(c, x, z);
}

However, the way I am declaring the color values randomly doesn't seem to be constant within the function, despite storing the values as variables; rather, the second background call is generating entirely new values for the RGB variables.

I want the z, x, and c variables to not change upon being called into the 2nd background() command, but can't quite figure it out.

I tried making other variables, using frameRate(1), and moving the delay(500) command past the 2nd background(), but unfortunately the variables change upon being called the 2nd time, and I want them to keep their value until the next void draw() loop.

答案1

得分: 1

以下是翻译好的部分:

即使使用 delay() 调用(我不建议这样做),第二次调用 background() 时,您会覆盖在第一次调用时设置的值。
您可能希望每帧更改一次颜色(而不是两次)。

如果您想在两个调用之间切换((z,x,c) / (c, x, z)),您可以使用一个单独的布尔值或表达式来在两者之间切换(从而避免在同一帧中两次更改背景)。

例如,您可以检查 frameCount 和取模(%)来查看帧是否交替。

例如:

if (frameCount % 2 == 0){   
  background(z, x, c);
}else{
  background(c, x, z);
}

警告:如果您的帧率很高,这实际上会看起来像闪烁。

鉴于 r、g、b 值本身是随机的,从感知上很难区分它们之间的差异。

(不清楚您试图实现的整体效果是什么,视觉上,但至少您在上面有一个关于如何在两种颜色之间切换的提示)

英文:

Even with the delay() call (which I don't recommend), the second time you call background() you'd overwrite the values set during the 1st call.
You probably want to change the colour once per frame (not twice).

If you'd like to toggle between the two calls ((z,x,c) / (c, x, z)) you can use a separate boolean value or expression to swap between the two (thus avoiding changing the background twice in the same frame).

For example you can check frameCount and the modulo(%) to check if when frames alternate.

e.g.

if (frameCount % 2 == 0){   
  background(z, x, c);
}else{
  background(c, x, z);
}

Warning: If you're on a high frame rate this will essentially look strobey / flashy.

Given the r,g,b values are random anyway, perceptually it will be hard to tell the difference.

(It's unclear what the overall effect you're trying to achieve is, visually, but at least you have a hint above on how you'd swap between two colours)

答案2

得分: 0

如果有一个名为initialized的布尔变量,其初始值为false,则可以检查其值并在其为false时将其设置为true。这将确保在第一次调用时初始化变量,但之后不会更改它们的值:

void draw() {
    if (!initialized) {
        z = float(random(255));
        x = float(random(255));
        c = float(random(255));
        initialized = true;
    }
    background(z, x, c);
    delay(500);
    background(c, x, z);
}
英文:

Assuming you have a boolean variable called initialized, with its initial value being false, then you can check for its value and set it to true if it was false. This will make sure your variables are initialized at first call, but will not change their value afterwards:

void draw() {
    if (!initialized) {
        z = float(random(255));
        x = float(random(255));
        c = float(random(255));
        initialized = true;
    }
    background(z, x, c);
    delay(500);
    background(c, x, z);
}

答案3

得分: 0

If you slow your demo way down it is easier to see what is happening. You can't change the background color in a single frame; you only get one shot/frame. But what you can do is set the background color only on the even frame counts and then use the swapped values for the odd frame counts. The console output confirms that the middle value stays the same while value 1 and value 3 swap places for the alternate background color.

float z, x, c;

void setup() {
  size(200, 200);
  background(255);
  frameRate(1);
}

void draw() {
  println("===============================");
  if (frameCount % 2 == 0) {
    z = random(255);
    x = random(255);
    c = random(255);
    println("1:", z, x, c);
    background(z, x, c);
    delay(5000);
  } else {
    println("2:", c, x, z);
    background(c, x, z);
    delay(5000);
  }  
}

console output:

变量使用”random()”每次调用时都会改变。

Addendum re: original post

I think the premise that the rgb values are changing during the life of a single frame is incorrect. The following slowed down version of the original post shows that the values do not change. In addition, the second background() call has no effect. The background color of the window does not change until a new frame is called and a new set of random r,g,b values are generated. This occurs with the first background() call only.

float z, x, c;

void setup(){
 frameRate(1); 
}

void draw() {
    z = random(255);
    x = random(255);
    c = random(255);
    println("1:", z, x, c);
    background(z, x, c);
    delay(5000);
    println("2:", c, x, z);
    background(c, x, z);
    println("====================");
    delay(5000);
}
英文:

If you slow your demo way down it is easier to see what is happening. You can't change the background color in a single frame; you only get one shot/frame. But what you can do is set the background color only on the even frame counts and then use the swapped values for the odd frame counts. The console output confirms that the middle value stays the same while value 1 and value 3 swap places for the alternate background color.

float z, x, c;

void setup() {
  size(200, 200);
  background(255);
  frameRate(1);
}

void draw() {
  println("===============================");
  if (frameCount % 2 == 0) {
    z = random(255);
    x = random(255);
    c = random(255);
    println("1:", z, x, c);
    background(z, x, c);
    delay(5000);
  } else {
    println("2:", c, x, z);
    background(c, x, z);
    delay(5000);
  }  
}

console output:

变量使用”random()”每次调用时都会改变。

Addendum re: original post

I think the premise that the rgb values are changing during the life of a single frame is incorrect. The following slowed down version of the original post shows that the values do not change. In addition, the second background() call has no effect. The background color of the window does not change until a new frame is called and a new set of random r,g,b values are generated. This occurs with the first background() call only.

float z,x,c;

void setup(){
 frameRate(1); 
}

void draw() {
    z = random(255);
    x = random(255);
    c = random(255);
    println("1:", z, x, c);
    background(z, x, c);
    delay(5000);
    println("2:", c, x, z);
    background(c, x, z);
    println("====================");
    delay(5000);
}

huangapple
  • 本文由 发表于 2023年4月13日 21:27:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定