Difference between revisions of "CSC111 Homework 4 2011"
(Created page with "--~~~~ ---- <center> <font size="+2">Page under construction!</font> <br \>300px </center> =Mondrian= right|200px Piet Mon...") |
(→Mondrian) |
||
Line 13: | Line 13: | ||
This assignment is inspired by this style and asks you to write a graphics program that will display 1000 random rectangles filled with random colors. | This assignment is inspired by this style and asks you to write a graphics program that will display 1000 random rectangles filled with random colors. | ||
+ | |||
+ | ==First, explore the Python '''random''' module== | ||
+ | |||
+ | * The random module is a standard module in Python. It is part of the language and can be used to generate randomness. Actually, pseudo-randomness, because all randomness generated by programs cannot be purely random, since it is always based on some mathematical algorithm. | ||
+ | |||
+ | * Visit the official python.org documentation on the [http://docs.python.org/py3k/library/random.html random module], and in particular scroll down to the [http://docs.python.org/py3k/library/random.html?highlight=random#examples-and-recipes Examples and Recipes] section. It shows many different ways to get random information. | ||
+ | |||
+ | * For example. If I wanted to pick a random color out of a set of colors, here's a way we could do it: | ||
+ | |||
+ | <source lang="python"> | ||
+ | import random | ||
+ | |||
+ | def main(): | ||
+ | colors = ["blue", "red" ] | ||
+ | |||
+ | for i in range( 10 ): | ||
+ | print( random.choice( colors ) ) | ||
+ | |||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | |||
+ | :I have used a for-loop just to test the ''choice()'' function several times, and verify that it picks a random color every time. | ||
+ | |||
+ | * By the way, a list of the color names that are recognized by the graphics module is available [[Tk Color Names|here]]. | ||
+ | |||
+ | * Add a few more colors to your list and run your program again. See that it picks them at random? |
Revision as of 11:53, 4 October 2011
--D. Thiebaut 12:42, 4 October 2011 (EDT)
Mondrian
Piet Mondrian is famous for his very geometric paintings with simple colors with mostly white, red, yellow and blue rectangles between black lines. This style has been applied to many different articles, as shown below.
This assignment is inspired by this style and asks you to write a graphics program that will display 1000 random rectangles filled with random colors.
First, explore the Python random module
- The random module is a standard module in Python. It is part of the language and can be used to generate randomness. Actually, pseudo-randomness, because all randomness generated by programs cannot be purely random, since it is always based on some mathematical algorithm.
- Visit the official python.org documentation on the random module, and in particular scroll down to the Examples and Recipes section. It shows many different ways to get random information.
- For example. If I wanted to pick a random color out of a set of colors, here's a way we could do it:
import random
def main():
colors = ["blue", "red" ]
for i in range( 10 ):
print( random.choice( colors ) )
main()
- I have used a for-loop just to test the choice() function several times, and verify that it picks a random color every time.
- By the way, a list of the color names that are recognized by the graphics module is available here.
- Add a few more colors to your list and run your program again. See that it picks them at random?