Computer Science Canada

Does anyone know how to randomize colours?

Author:  lawyer_chelsea [ Sat Oct 01, 2005 7:17 pm ]
Post subject:  Does anyone know how to randomize colours?

I have an assignment due and I can't for the life of me remember how to randomize colours. Could someone tell me the code or where I can find it?
It would be greatly appreciated!! Thanks!! Very Happy

Author:  [Gandalf] [ Sat Oct 01, 2005 7:34 pm ]
Post subject: 

You see, I would help you, but I don't really know the answer since I'm only doing a compsci "gig", while my real job is stand up comedy (how did you know?).

Read the rules. At least you've followed my advice and stopped overusing smilies. Good job!!

Author:  Cervantes [ Sat Oct 01, 2005 7:39 pm ]
Post subject: 

You don't "randomize the colours". Well, you could, but that would be a very inefficient way to do it. Instead, just generate a random number, using Rand.Int(), and then use that number as the colour number.

Author:  Tony [ Sat Oct 01, 2005 11:11 pm ]
Post subject: 

[Gandalf] wrote:
At least you've followed my advice and stopped overusing smilies. Good job!!

Now if you can only teach lawyer_chelsea to use more contrasting text colours... Laughing

Author:  Hikaru79 [ Sun Oct 02, 2005 12:26 am ]
Post subject: 

code:
loop
     Draw.FillOval (maxx div 2, maxy div 2, 50, 50, Rand.Int(1,256))
end loop

There is an INTENTIONAL BUG in there just because I don't want you copying and pasting this if its an assignment. If you understand what I did, you'll understand what to fix. Enjoy Smile

Author:  codemage [ Mon Oct 03, 2005 8:15 am ]
Post subject: 

Maybe you've already been doing it, but your absent-minded teacher forgot to install the Draw.Random.Colour(# of random colours) library.

Wink

Author:  TokenHerbz [ Mon Oct 03, 2005 1:04 pm ]
Post subject: 

i just do this for random colors

code:

var color: int

loop
     randint(color,1,255)
     drawfilloval(20,20,20,20,color)
end loop


Then again, i seem to make more work the is nessesary...

Author:  jamonathin [ Mon Oct 03, 2005 1:16 pm ]
Post subject: 

Hikaru79 wrote:
code:
loop
     Draw.FillOval (maxx div 2, maxy div 2, 50, 50, Rand.Int(1,256))
end loop

There is an INTENTIONAL BUG in there just because I don't want you copying and pasting this if its an assignment. If you understand what I did, you'll understand what to fix. Enjoy Smile

Or, we could just use [Censored: Hikaru79 had his reasons] No more bug Razz
tokenherbz wrote:
Then again, i seem to make more work the is nessesary...

For the most part, a lazy programmer makes a good programmer, code wise, not work ethic wise.

Author:  Cervantes [ Mon Oct 03, 2005 4:43 pm ]
Post subject: 

tokenherbz wrote:
i just do this for random colors

code:

var color: int

loop
     randint(color,1,255)
     drawfilloval(20,20,20,20,color)
end loop


Then again, i seem to make more work the is nessesary...

Yeah, you did in this situation as well. Use functions over procedures. The better way to do this is seen in Hikaru's post. Smile

Author:  TokenHerbz [ Tue Oct 04, 2005 1:22 am ]
Post subject: 

why's a function better then a proc?

what are the differenc's?

Author:  Tony [ Tue Oct 04, 2005 7:37 am ]
Post subject: 

a procedure is a function that doesn't return a value.
brief comparison

Author:  codemage [ Tue Oct 04, 2005 7:47 am ]
Post subject: 

That question was asked really recently in another thread... but I can't seem to find it.

Functions return a value, procs don't.
Because of that, you can write tighter, better code with functions - and they enable recursion.

Author:  Tony [ Tue Oct 04, 2005 8:20 am ]
Post subject: 

codemage wrote:
Because of that, you can write tighter, better code with functions - and they enable recursion.

It's the other way around. You can't write tighter, better code with procedures. Wink

Functions do enable recursion though.

Author:  codemage [ Tue Oct 04, 2005 9:06 am ]
Post subject: 

Shocked I write tighter code with functions all the time.

Here's an elementary program which does the same thing with both a function and a procedure. The section which uses the function does in 1 line what it takes 2 lines for the procedure to do.

code:

var test1 : int := 5

function doubleit (num : int) : int
    result num * 2
end doubleit

procedure doubleit2 (var num : int)
    num := num * 2
end doubleit2

%Double & put a number with a function
put doubleit(test1)

%Double & put a number with a procedure
doubleit2(test1)
put test2

Author:  beard0 [ Tue Oct 04, 2005 9:34 am ]
Post subject: 

codemage wrote:
you can write tighter, better code with functions

Tony wrote:
You can't write tighter, better code with procedures.


Tony is talking about a slight nuance, but saying the same thing in the end - in your example, you could just have well written
code:
put num * 2
(1 line) which, incidentally, would actually cause less instruction cycles in the processor than your function call, meaning that your use of a function has not actually made tighter code.

Author:  codemage [ Tue Oct 04, 2005 12:00 pm ]
Post subject: 

Just trying to show the difference between the two.

The nuance makes more sense now, with the Freudian slip changed.

Tony had written:
You can't write tighter, better code with functions.

...either that, or my eyes (or mind) are going.

Anyhow - as long as tokenherbz understands the difference...

Author:  Tony [ Tue Oct 04, 2005 12:49 pm ]
Post subject: 

what the? Shocked I think somebody is playing a joke on me there.

Anyways, functions are where it's at, since procedures are just functions that don't return a value.

Turing:

procedure(var x)
procedure(var y)
call(x,y)

vs
Turing:

call(function(x), function(y))


though ultimatly your main aim is for redability of your code. Just because you can compact your program down to twenty lines of nested chain functions, doesn't mean you have to.

Author:  Mazer [ Tue Oct 04, 2005 2:57 pm ]
Post subject: 

codemage wrote:
Because of that, you can write tighter, better code with functions - and they enable recursion.

I'm pretty sure recursion is possible with procedures. If anything, they just won't allow as much freedom as you get with recursive functions.

Author:  [Gandalf] [ Tue Oct 04, 2005 3:16 pm ]
Post subject: 

This is a very useless example of that:
code:
var product, numA, numB, cnt : int := 0

proc multiply (numA, numB : int)
    product += numA
    cnt += 1
    if cnt ~= numB then
        multiply (numA, numB)
    end if
end multiply

multiply (5, 5)
put product


But then it comes back to, procedures are functions.


: