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)
   *flash guy warning* convert a Char() to color()?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: *flash guy warning* convert a Char() to color()?  (Read 1355 times)
lunetta

7200475272004752 WWW Email
*flash guy warning* convert a Char() to color()?
« on: Mar 22nd, 2004, 7:00am »

Hello to all
 
First of all, I'm really sorry to bother you all with this newbie stuff. I can even suggest a "newbie" section on the forum, so these questions won't mix with the serious stuff.
 
 
I have a code that defines three colors:
 
  color c1 = color (225, 15,10);
  color c2 = color (35, 200, 5);
  color c3 = color (35, 120, 120);
 
then I'm drawing some stuff; the fill of these stuff needs to be one of those 3 predefined colors, at random; or c1, or c2 or c3;
 
1) is it possible to do something like
 
String randc = "c"+int(random(3)+1);
 
and then use the String randc as a color fill -  fill(randc); ?? how??
 
2) I actually coded this using a array; Is this the best way?
 
  colorMode(RGB, 255);
  color[] carray = new color[3];
  carray[0]=color(255,0,0);
  carray[1]=color(0,255,0);
  carray[2]=color(0,0,255);
  fill(carray[int(random(3))]);
 
(this is just an example; the I would not leave these things together, because the loop would redefine the array everytime and the speed would be wasted; the array needs to be globally declared, the values assined into the setup void and the fill should stay on the loop... anyway, what I really want to know is if it's possible to evaluate a string and do not use arrays...)
 
Really thanks for all!
 
der_rabe
Guest
Email
Re: *flash guy warning* convert a Char() to color(
« Reply #1 on: Mar 22nd, 2004, 10:00am »

hi lunetta
 
first of all: you don't bother anyone, i guess. processing is an learning-environment by definition. we are all on different levels of knowledge and if we help each other to learn, we fight the overwhelming stupidity in the world. so: no worries.
 
your problem: i am not sure about the use of a String in the color-funktion, but since color() needs a color-object i tend to say that it won't accept a string.
i did not try it. i am actually not on my own pc.
 
your array looks fine, except for the fact that you should use random(2) and not random(3).
 
TomC

WWW
Re: *flash guy warning* convert a Char() to color(
« Reply #2 on: Mar 22nd, 2004, 11:48am »

on Mar 22nd, 2004, 7:00am, lunetta wrote:
Hello to all
 
First of all, I'm really sorry to bother you all with this newbie stuff. I can even suggest a "newbie" section on the forum, so these questions won't mix with the serious stuff.
 

 
What serious stuff
 
on Mar 22nd, 2004, 7:00am, lunetta wrote:

 
1) is it possible to do something like
 
String randc = "c"+int(random(3)+1);
 
and then use the String randc as a color fill -  fill(randc); ? how?
 

 
Not in a way that wouldn't give you an enormous headache and simultaneously throw away the advantages of moving from scripting to compiling
 
Evaluating Strings at runtime is a very ECMAScripty (ActionScripty / JavaScripty) way of doing things.  
 
I have written a function which does what you ask, but I wouldn't recommend using it, because your Java code is the Right Way to do it.  If you're interested, it's here: http://processing.org/discourse/yabb/board_Proce55ing_Software__action_display_num_1079959163_start_0.html
 
 
 
on Mar 22nd, 2004, 7:00am, lunetta wrote:

 
2) I actually coded this using a array; Is this the best way
 
  colorMode(RGB, 255);
  color[] carray = new color[3];
  carray[0]=color(255,0,0);
  carray[1]=color(0,255,0);
  carray[2]=color(0,0,255);
  fill(carray[int(random(3))]);
 

 
Yes.  That's the compiled world's way of doing the same thing.  
 
But... der_rabe has already expressed concern about the random(3)... I have concerns too.  
 
random(3) will give back a random value between 0.0 and 3.0 - this could, rarely, give you a 3.0  Then your code would crash.
 
 
 
random(3)...
 
     |--------|--------|--------|
     0        1        2        3
    0.0 ---------------------> 3.0
 

 
Try the following code...
 
Code:

 
int count = 0;
 
while (int(random(3)) != 3) {
  count++;
}
 
print(count);
 

 
When I tried it, it took 21 million calls to random(3) before it gave me a 3 - but it could have been one call
 
I think a quick way around this is to use random(2.999999999999999), because this should never give you 3, and 2.99999999999 converted to an int is 2.  So you can see that int(random(2.999999999)) gives you 0, 1, or 2 an (approximately) equal number of times, on average.
 
Code:

 
int[] count = new int[3];
 
for (int i = 0; i < 1000000; i++) {
  int index = int(random(2.999999999));
  count[index]++;
}
 
for (int i = 0; i < count.length; i++) {
  println("count[" + i + "]: " + count[i]);
}
 

« Last Edit: Mar 22nd, 2004, 1:41pm by TomC »  
fry


WWW
Re: *flash guy warning* convert a Char() to color(
« Reply #3 on: Mar 22nd, 2004, 2:28pm »

i ran into this with random() in a project over the weekend. seems as though it might be better to have random(3) be non-inclusive of 3. any language weenies out there know specifically how this is dealt with in other programming languages?
 
TomC

WWW
Re: *flash guy warning* convert a Char() to color(
« Reply #4 on: Mar 22nd, 2004, 2:53pm »

The language weenies who designed the Random class is Java agree with you.  Their nextInt(int n) method returns a value between 0 (inclusive) and n (exclusive).  
 
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html#nextInt(in t)
 
Perhaps a randomInt(int n) function would be handy? That would mean the following would be safe:
 
Code:

fill( carray[randomInt(carray.length)] );

 
I don't think you should change the behaviour of random, since it's common to want an inclusive range of continuous values.  When it comes to discrete values though, it seems common to want a uniform distrubution of them, and asking for int(random(0.0,2.9999999)) to get near to that is yucky.
 
On update: Actually, it looks like the Java Random.nextFloat() method runs from 0.0 (inclusive) to 1.0 (exclusive) so maybe inclusive ranges aren't all that.
« Last Edit: Mar 22nd, 2004, 2:57pm by TomC »  
fry


WWW
Re: *flash guy warning* convert a Char() to color(
« Reply #5 on: Mar 22nd, 2004, 3:33pm »

yeah, i can think of a lot of ways to use an exclusive version, but many fewer for the inclusive. i think the original thinking in making it inclusive was the confusion of telling beginners that random(3) meant 0 to 2.. but since it *effectively* means 0..2 anyway, then that's just something people will have to learn.. Math.random() is also [0, 1), so i'm actually surprised that we're returning the occasional inclusive value. maybe that's err, a bug.
 
lunetta

7200475272004752 WWW Email
Re: *flash guy warning* convert a Char() to color(
« Reply #6 on: Mar 22nd, 2004, 6:47pm »

Really thanks for all answers!
 
I wasn't really sure if random() was inclusive or not...
 
Thanks all!
 
fry


WWW
Re: *flash guy warning* convert a Char() to color(
« Reply #7 on: Mar 28th, 2004, 9:54pm »

a fix for this can be found in rev 69, though the release is "use at your own risk".
 
Pages: 1 

« Previous topic | Next topic »