Concepts / Randomness in Programs

Randomness in Programs

SimpleGraphicsStarter.java is the text's first example of a GUI program, meaning a program whose result is a graphical interface or picture.

  • Programming

A Question to Hold in Mind

Most of the programs you have written so far probably communicated through text: a line of output in a console, a number printed, a message typed back to the user. This article introduces a different kind of program, one whose result is a picture that appears in a window. The example is called SimpleGraphicsStarter.java, which comes from Section 3.9 of the text. Its job is easy to state: it draws a large number of disks, and each disk is randomly colored and randomly positioned.

The question this article wants you to be able to answer is simple to ask and worth asking carefully: after the program has finished drawing, what does the graphics window look like? Keep that question in mind as you read. The rest of the article builds the answer step by step, starting from the idea of a graphical user interface and ending with a before and after view of the window.

What do you think happens?

Before the program draws anything, how many disks are in the graphics window?

Reveal answer
Answer:

None. Before the drawing, the window has no disks in it.

The window only gains disks as the program draws them. The change from an empty window to a window full of disks is the whole effect of the program.

Programs That Produce Pictures

Definition

A GUI program is a program whose result is a graphical interface or picture. GUI stands for graphical user interface. SimpleGraphicsStarter.java is the text's first example of a GUI program.

The contrast with text-based programs is the key idea. A text program's result is words or numbers. A GUI program's result is something you look at: a window whose contents are the point of the program. The text introduces SimpleGraphicsStarter.java at this point because the program is small enough that the whole idea can be seen at once. It produces a graphical result, and that result is what the program is about.

Later programs in the text build on this same idea, so it is worth understanding this first example clearly before adding more complexity. When you read a GUI program, the first useful question is not how the code is written but what the window will contain when the program is done.

What SimpleGraphicsStarter Draws

The defining feature of this program is that it generates graphics with random properties. The source describes the program as drawing a large number of disks, and each disk is randomly colored and randomly positioned. The word randomly applies to two properties of each disk: its color and its location in the window.

Because the properties are random, the program does not draw one fixed picture that you could predict in advance. It draws many disks, and each one has its own color and its own place. Taken together, the drawing is a collection of disks that all differ from one another. Thinking of the final picture as a collection, rather than as a single object, is the key to reading the program's result.

Tracing the Window Before and After Drawing

The figure below shows the window at two moments. Before the drawing, the window has no disks in it. After the program has drawn, the window holds a large number of disks. Each of those disks has its own color and its own position, which is what the source means by randomly colored and randomly positioned. The change from the first state to the second is the whole effect of the program.

Empty windowno disks yetOne diskone random color andpositionSeveral diskseach with its own color andpositionLarge number of disksthe finished picturedraw a diskdraw more disksdrawing finishes
What does the window look like before any disks are drawn, and what changes as each random disk is added until the program finishes?
Worked example

Reading the window at two moments

A student runs SimpleGraphicsStarter and wants to describe what changed in the window from start to finish.

  1. 1
    Describe the starting state

    Before drawing begins, the window contains no disks. This is the reference point for measuring change.

  2. 2
    Describe the ending state

    After the program finishes, the window holds a large number of disks, and each disk has its own color and position.

  3. 3
    Name what changed

    The change is the addition of many disks. The program's effect is the transition from an empty window to a crowded one.

  4. 4
    Describe properties, not an exact picture

    Because color and position are random, the student describes the kinds of properties the disks have rather than a fixed arrangement.

Result

The window goes from empty to containing a large number of disks, each randomly colored and randomly positioned.

Why Each Run Looks Different

The program's code does not change between runs, but the picture does. The randomness is what makes that possible. Each disk's color and position are chosen randomly, so two separate runs of the same program can produce disks with different colors in different places. The code is identical each time; the properties of the output are not.

Run onemany disks, random colorsand positionsRun twomany disks, differentrandom colors and positions
How do two separate runs of SimpleGraphicsStarter differ in disk colors and positions even though the code is identical?

The two runs share the same kind of result: a large number of disks, each with a color and a position. What differs is the specific values. This is why the useful description of the output is about its kinds of properties rather than one exact image.

Describing Properties Instead of a Fixed Picture

Best practice

When you describe the output of a program that uses randomness, state the properties the output is guaranteed to have, such as a large number of disks with each disk randomly colored and randomly positioned. Avoid describing one specific arrangement as if it would appear on every run.

This habit matters because it matches what the program actually promises. A fixed-picture description would be false on most runs. A property-based description stays true across runs, even though every run looks different.

Mistakes Learners Make with Random Graphics

  • Common mistake

    Expecting the same picture every time the program runs

    What it looks like

    A learner runs SimpleGraphicsStarter, sketches the disks, and then is surprised that the next run shows different colors in different places.

    Why it fails

    Each disk's color and position are random, so the output is not fixed by the code alone.

    Do this instead

    Describe the kinds of properties the picture has, such as many disks, each randomly colored and randomly positioned.

  • Common mistake

    Thinking the window is the same before and after drawing

    What it looks like

    A learner assumes the window already contains disks when the program starts.

    Why it fails

    Before the drawing, the window has no disks in it. The disks appear as the program draws them.

    Do this instead

    Compare the window before drawing with the window after drawing, and treat the change as the program's effect.

  • Common mistake

    Treating one disk as the whole picture

    Why it fails

    The source describes the final picture as a collection of disks with random properties, not a single object.

    Do this instead

    Think of the picture as many disks taken together.

Trace the Window Yourself

EASY

A friend says that SimpleGraphicsStarter always draws the same picture because the code never changes. Using the before and after view of the window, explain why your friend is wrong, and describe what you would expect to see on two separate runs.

Hints
  • Think about what the code does not change and what the program randomly chooses.
  • Compare the empty window at the start with the window after drawing.
  • Describe the properties the two runs share, not the exact positions.

Key Takeaways

  • A GUI program is a program whose result is a graphical interface or picture, and SimpleGraphicsStarter.java is the text's first example of one.
  • SimpleGraphicsStarter draws a large number of disks, and each disk is randomly colored and randomly positioned.
  • Reading the result means comparing the graphics window before drawing, when it has no disks, with the window after drawing, when it holds many disks.
  • Because color and position are random, each run produces different specific values, so the output should be described by its properties rather than as one fixed picture.