Random Numbers and Simulations in CS106A, Exercises of Programming Methodologies

Handouts for three different java programs from the cs106a course at stanford university. The first program, rolldice.java, simulates rolling multiple dice until the maximal value is rolled. The second program, colorchangingsquare.java, displays a square that changes color randomly every second. The third program, piapproximation.java, approximates the value of pi using monte carlo integration by simulating the throwing of darts at a virtual dartboard.

Typology: Exercises

2010/2011

Uploaded on 10/06/2011

hollyb
hollyb 🇺🇸

4.8

(44)

431 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Mehran Sahami Handout #16
CS 106A October 10, 2007
Examples of Programs Using Random Numbers
Portions of this handout by Eric Roberts
/*
* File: RollDice.java
* -------------------
* This program simulates rolling some number of dice until
* the maximal value on the all the dice is rolled.
*/
import acm.program.*;
import acm.util.*;
public class RollDice extends ConsoleProgram {
/* Number of sides on each die */
private static final int NUM_SIDES = 6;
public void run() {
int numDice = readInt("Number of dice: ");
int maxRoll = numDice * NUM_SIDES;
int numRolls = 0;
while (true) {
int roll = rollDice(numDice);
numRolls++;
if (roll == maxRoll) break;
println("Rolled " + roll);
}
println("Rolled " + maxRoll + " after " + numRolls + " rolls");
}
/* Returns the total of rolling numDice dice */
private int rollDice(int numDice) {
int total = 0;
for (int i = 0; i < numDice; i++) {
total += rgen.nextInt(1, NUM_SIDES);
}
return total;
}
/* Private instance variables */
private RandomGenerator rgen = RandomGenerator.getInstance();
}
pf3

Partial preview of the text

Download Random Numbers and Simulations in CS106A and more Exercises Programming Methodologies in PDF only on Docsity!

Mehran Sahami Handout

CS 106A October 10, 2007

Examples of Programs Using Random Numbers

Portions of this handout by Eric Roberts /*

  • File: RollDice.java

  • This program simulates rolling some number of dice until
  • the maximal value on the all the dice is rolled. / import acm.program.; import acm.util.; public class RollDice extends ConsoleProgram { / Number of sides on each die / private static final int NUM_SIDES = 6; public void run() { int numDice = readInt("Number of dice: "); int maxRoll = numDice * NUM_SIDES; int numRolls = 0; while (true) { int roll = rollDice(numDice); numRolls++; if (roll == maxRoll) break; println("Rolled " + roll); } println("Rolled " + maxRoll + " after " + numRolls + " rolls"); } / Returns the total of rolling numDice dice / private int rollDice(int numDice) { int total = 0; for (int i = 0; i < numDice; i++) { total += rgen.nextInt(1, NUM_SIDES); } return total; } / Private instance variables */ private RandomGenerator rgen = RandomGenerator.getInstance();
  • 2 – /*
  • File: ColorChangingSquare.java

  • This program puts up a square in the center of the window
  • and randomly changes its color every second. / import acm.graphics.; import acm.program.; import acm.util.; public class ColorChangingSquare extends GraphicsProgram { /* Size of the square in pixels / private static final int SQUARE_SIZE = 100; / Pause time in milliseconds / private static final int PAUSE_TIME = 1000; public void run() { GRect square = new GRect(SQUARE_SIZE, SQUARE_SIZE); square.setFilled(true); add(square, (getWidth() - SQUARE_SIZE) / 2, (getHeight() - SQUARE_SIZE) / 2); / Note: we meant to have this infinite loop / while (true) { square.setColor(rgen.nextColor()); pause(PAUSE_TIME); } } / Private instance variables */ private RandomGenerator rgen = RandomGenerator.getInstance();