Computer Science Canada

Random Colours

Author:  cam [ Sat Nov 11, 2006 2:49 pm ]
Post subject:  Random Colours

I want to do 3 colours switching from red to black to green. I want to do this as a variable.

Author:  richcash [ Sat Nov 11, 2006 3:11 pm ]
Post subject: 

Something like this?
code:
var c : array 1 .. 3 of int := init (red, black, green)
var clr : int
loop
     clr := Rand.Int (c, 1, 3)

end loop
That's if you want it random, as the post title suggests.

This could be for swithching in an orderly fashion.
code:
var c : array 1 .. 3 := init (red, black, green)
var n := 0
var clr : int
loop
    n += 1
    clr := c (n mod 3 + 1)
end loop

Author:  zylum [ Sun Nov 12, 2006 2:52 am ]
Post subject: 

both your code examples dont work Confused

to choose one of those three colours at random you could do:

code:
round (Rand.Int (1, 3) * 2.2)


Laughing

Author:  TokenHerbz [ Sun Nov 12, 2006 5:49 am ]
Post subject: 

zylum, would those colors work with whatdotcolor red, black, green?

perhaps later he would want to use a different variety of colors.

Author:  richcash [ Sun Nov 12, 2006 12:10 pm ]
Post subject: 

Oops, I must've deleted too much when I took out my drawing stuff. Here :
switching orderly :
code:
var c : array 1 .. 3 of int := init (red, black, green)
var n := 0
var clr : int
loop
    n += 1
    clr := c (n mod 3 + 1)
    Draw.FillOval (50, 50, 15, 15, clr)
    delay (200)
end loop


randomly :
code:
var c : array 1 .. 3 of int := init (red, black, green)
var clr : int
loop
    clr := c (Rand.Int (1, 3))
    Draw.FillOval (50, 50, 15, 15, clr)
    delay (200)
end loop


Use zylum's way for simplicity, but use mine if you want to change the colours later on (or come up with your formula!).

Author:  cam [ Sun Nov 12, 2006 1:43 pm ]
Post subject: 

alright thx

Author:  zylum [ Sun Nov 12, 2006 4:44 pm ]
Post subject: 

nah, dont use my way Laughing

the code is not self explanitory and you cant extend it to use other colours. i was just fooling around. unless you are trying to ubfuscate your code then by all means use it Wink


: