Stanford CS106A Section Assignment Random Circles Solution

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<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 🙂

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.