FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   0067: Strange 'if' problem...
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: 0067: Strange 'if' problem...  (Read 260 times)
rgovostes

rgovostes
0067: Strange 'if' problem...
« on: Dec 20th, 2003, 2:27am »

I have this nugget of code in my program:
 
Code:
class EssWave {
  float mAcceleration = random(1.0);
  if (random(2) == 1) { mAcceleration = 1.0 / (1.0 / mAcceleration); }
  float mFlipSign = 1.0 + (rnd(2) * -2.0);
   
  float Value(float d) {
    return ((2.0 / (mAcceleration * sq(d) + 1.0)) - 1.0) * mFlipSign;
  }
}

 
Nothing too complicated... When you instantiate the class, it picks a few random values for an EssWave, then lets you find values of positions on the wave (think of it as f(x) ...)
 
Now, my problem is that it expects a right curly bracket before the 'if' statement. If I change the top part to just the following:
 
Code:
class EssWave {
  float mAcceleration = random(1.0);
  mAcceleration = 1.0 / (1.0 / mAcceleration);

 
It doesn't know what mAcceleration is! I cannot figure out what the problem is, and I was tempted to post this under the Software Bugs section - but, how would something this big escape others?
 
Please help! Thank you in advance,
Ryan Govostes
 
rgovostes

rgovostes
Re: 0067: Strange 'if' problem...
« Reply #1 on: Dec 20th, 2003, 5:48am »

Apparently you need to have code that executes when you instantiate the object. So, the following runs successfully:
 
Code:
class EssWave {
  float mAcceleration;
  float mFlipSign;
   
  EssWave() {
    mAcceleration = random(1.0);
    if (random(2) == 1) { mAcceleration = 1.0 / (1.0 / mAcceleration); }
    float mFlipSign = 1 + (random(2) * -2);
  }
   
  float Value(float d) {
    return ((2.0 / (mAcceleration * sq(d) + 1.0)) - 1.0) * mFlipSign;
  }
}

 
However, I'm not sure why random(2) returns a float, as it is passed an integer.
 
Bijeoma

myloveiloved
Re: 0067: Strange 'if' problem...
« Reply #2 on: Dec 20th, 2003, 10:20pm »

random(2) returns a float between 0.0-2.0
(int) random(2) will return 0 1 or 2.
« Last Edit: Dec 20th, 2003, 10:23pm by Bijeoma »  
TomC

WWW
Re: 0067: Strange 'if' problem...
« Reply #3 on: Dec 21st, 2003, 12:42am »

on Dec 20th, 2003, 10:20pm, Bijeoma wrote:
random(2) returns a float between 0.0-2.0
(int) random(2) will return 0 1 or 2.

 
Erm... true.  I think eventually you might get a 2 from that.  But you're way more likely to just get a 1 or a 0.
 
Example:
 
Code:

void setup() {
  int zeroes = 0;
  int ones = 0;
  int twos = 0;
  int r = 0;
  while (twos == 0) {
    r = (int)random(2);
    if (r == 0) zeroes++;
    else if (r == 1) ones++;
    else if (r == 2) twos++;
  }
  println("zeroes : " + zeroes);
  println("ones   : " + ones);
  println("twos   : " + twos);
}

 
For 4 sample runs I get...
 
zeroes : 14948867
ones   : 14951540
twos   : 1
 
zeroes : 19445847
ones   : 19446226
twos   : 1
 
zeroes : 34047315
ones   : 34045484
twos   : 1
 
zeroes : 6974995
ones   : 6977114
twos   : 1
« Last Edit: Dec 21st, 2003, 12:43am by TomC »  
Bijeoma

myloveiloved
Re: 0067: Strange 'if' problem...
« Reply #4 on: Dec 21st, 2003, 12:48am »

true i shouldve pointed that out
« Last Edit: Dec 21st, 2003, 12:49am by Bijeoma »  
Pages: 1 

« Previous topic | Next topic »