Fundamentals of Computer Science I (CS151.02 2007S)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
This lab is also available in PDF.
Summary: In this laboratory, we explore Scheme's
random
procedure and its use in some simple simulations.
Contents:
random
a. Evaluate the expression (random 10)
twenty times.
What values do you get?
b. What values do you expect to get if you call random
with
1 as a parameter?
Check your hypothesis experimentally.
c. What do you expect to happen if you call random
with 0
or 1 as a parameter?
Check your hypothesis experimentally.
d. Try calling random
with other parameters. What effect
does the parameter seem to have?
e. What is the largest integer you can provide as a parameter to
random
?
a. Copy the rolladie
and rolldice
procedures
from the reading. You can also find them
at the end of this lab.
b. Using rolldice
, roll ten dice.
c. Using rolldice
, roll ten dice. (Yes, this instruction
is the same as the previous instruction. You should do it twice.)
d. Did you get the same list of values each time?
e. What other procedures have you encountered that may return different values each time you call them with the same parameters?
Write a procedure, (countoddrolls n)
that counts the number of odd numbers that come up when rolling n
sixsided dice.
> (countoddrolls 10) 7 > (countoddrolls 10) 2 > (countoddrolls 10) 6 > (countoddrolls 10) 5
rolldice
Discuss with your partner how you might write a test suite for
rolldice
. Be prepared to share your answer with the
class.
a. Write a zeroparameter procedure, (heads?)
that simulates
the flipping of a coin. Heads should return #t
(which
represents "the coin came up heads") half the time and #f
(which represents "the coin came up tail") about half the time.
> (heads?) #f > (heads?) #f > (heads?) #t > (heads?) #t > (heads?) #f
b. Write a procedure, (countheads n)
that simulates
the flipping of n coins (using heads?
to simulate
each coin) and returns the number of times the coin is tails.
c. Use countheads
to test heads?
by counting
the number of heads you get in 1000 flips.
a. Write a procedure, (pairadice)
, that simulates the rolling
of two sixsided dice and prints out a pair of the results.
b. Write a procedure, (sumadice)
, that simulates the
rolling of two sixsided dice and then computes their sum.
c. Write a procedure, (countsevens n)
that simulates
the rolling of n pairs of dice and counts the number of times
the value 7 appears.
Consider the problem of rolling a pair of dice
n
times and counting the number of times that either a 7 or an 11
comes up.
a. What is wrong with the following procedure to accomplish this task?
(define sevenor11 (lambda (n) (cond ((<= n 0) 0) ((or (= (sumofdice) 7) (= (sumofdice) 11)) (+ 1 (sevenor11 ( n 1)))) (else (sevenor11 ( n 1))))))
Hint: Try adding a display
to sumofdice
so
that you can see how many times sumofdice
is called.
(If you still cannot figure it out after trying that, read the
the notes on this problem.
b. Write a correct procedure to solve this problem.
Extend your procedure from the previous exercise to count the number
of times 7, 11, or doubles
(two dice with the same value)
come up in n rolls.
a. Write a procedure, (randomstudent)
, that randomly
selects the name of a student from this class.
b. Write a procedure, (randompair)
, that randomly makes
a list of two students from the students of this class.
c. What is the potential problem with me using (randompair)
to select partners from this class?
Just in case you don't have the reading handy, here's the code again.
;;; Procedure: ;;; rolladie ;;; Parameters: ;;; None ;;; Purpose: ;;; To simulate the rolling of one sixsided die. ;;; Produces: ;;; An integer between 1 and 6, inclusive. ;;; Preconditions: ;;; None. ;;; Postconditions: ;;; Returns an integer between 1 and 6, inclusive. ;;; It should be difficult (or impossible) to predict which ;;; number is produced. (define rolladie (lambda () (let ((tmp (random 6))) ; tmp is in the range [0 .. 5] (+ 1 tmp)))) ; result is in the range [1 .. 6] ;;; Procedure: ;;; rolldice ;;; Parameters: ;;; n, an integer (the number of dice to roll) ;;; Purpose: ;;; Roll n dice. ;;; Produces: ;;; A list of integers, each between 1 and 6 (inclusive). ;;; Preconditions: ;;; n >= 1. ;;; Postconditions: ;;; Returns a list of length n. ;;; Each element of the list is between 1 and 6 (inclusive). ;;; The elements of the list are difficult (or impossible) to predict. (define rolldice (lambda (n) ; If there are no dice left to roll, ... (if (<= n 0) ; then give an empty list of rolls null ; Otherwise, roll once and then roll n1 more times. (cons (rolladie) (rolldice ( n 1))))))
a. You may note that the following line involves two calls to
the sumofdice
procedure.
((or (= (sumofdice) 7) (= (sumofdice) 11))
Since each call to sumofdice
involves a roll of the
dice, this code says, in effect, roll the dice and check if
the value is seven; if not roll them again and check if the value
is eleven
. However, we really want only one roll.
b. The solution is to use a let
clause.
(let ((roll (sumofdice))) ... ((or (= roll 7) (= roll 11))
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/History/Labs/simulation.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Sep 13 20:54:29 2007.
The source to the document was last modified on Thu Mar 1 21:07:49 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Labs/simulation.html
.
You may wish to validate this document's HTML ; ;
Samuel A. Rebelsky, rebelsky@grinnell.eduhttp://creativecommons.org/licenses/bync/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.