Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Random Colours
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cam




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Sat Nov 11, 2006 3:11 pm   Post subject: (No 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
zylum




PostPosted: Sun Nov 12, 2006 2:52 am   Post subject: (No 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
TokenHerbz




PostPosted: Sun Nov 12, 2006 5:49 am   Post subject: (No subject)

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

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




PostPosted: Sun Nov 12, 2006 12:10 pm   Post subject: (No 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!).
cam




PostPosted: Sun Nov 12, 2006 1:43 pm   Post subject: (No subject)

alright thx
zylum




PostPosted: Sun Nov 12, 2006 4:44 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: