Stanford CS106A Section Assignment Random Circles Solution

10 Nov

I’ve noticed that the Section Assignments are not nearly as hard as the homework assignment, which makes them kind of fun. And the Random Circle problem makes me really happy. Not only was I able to solve it pretty quickly, it’s really fun to run it and see a bunch of random circles just appear! Here is the Random Circles Problem from Section Assignment 3:

Here is my solution:

You can find the official solution here, while my solution is also on github.

/* This program draws a set of ten circles with different sizes, positions,
* and colors.  Each circle should have a randomly chosen color, a randomly chosen radius
* between 5 and 50 pixels, and a randomly chosen position on the canvas, subject to the
* condition that the entire circle must fit inside the canvas without extending past the edge.

import acm.program.*;
import acm.util.*;

public class RandomCircles extends GraphicsProgram {

	private RandomGenerator rgen = RandomGenerator.getInstance();
	private static final int numberOfCircles = 10;
	private static final int minRadius = 5;
	private static final int maxRadius = 50;

	public void run() {
		for(int i=0; i&lt;numberOfCircles; i++ ) {

	private void drawCircle() {
		double radius = rgen.nextDouble(minRadius, maxRadius);
		double x = rgen.nextDouble(0, getWidth() - radius*2);
		double y = rgen.nextDouble(0, getHeight() - radius*2);
		GOval circle = new GOval(x, y, radius, radius);


And here are some of my circles 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s