DOC PREVIEW
TAMU CSCE 110 - Simulations
Type Lecture Note
Pages 2

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110 1nd EditionLecture 11Outline of Last Lecture:I. Comma-Separated ValuesII. Immutable vs. MutableIII. IterablesA. For loopsB. MembershipOutline of Current Lecture:I. SimulationsA. DefinitionB. ExampleC. ExplanationCurrent Lecture:I. SimulationsA. We can create programs that will tell us more about real-world problems and situations by simulating experiments with Python. For example, we can write a program that tells us how many times, on average, how many times a person would need to roll a die before all six sides appeared. See the program below:B.1 >>> import random2 >>> def roll_die():3 >>> return random.randint(1,6)4 >>> def single_trial:5 >>> face = [0]*76 >>> count = 07 >>> while face[1] == 0 or face[2] == 0 or face[3] == 0 or face[4] == 0 or face[5] == 0 or face[6] == 0:8 >>> face[roll_die()] += 19 >>> count += 110 >>> return count11 >>> def main():12 >>> total = 013 >>> for i in range(0,1000):14 >>> total += single_trial()15 >>> print "Average number of rolls: ", total/1000These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.16 >>> main()C.Line Explanation1 Allows us to pull a random number later on in the code, which will represent the act of rolling a die and randomly getting a number 1-62-3 Writing a function that represents rolling a die in real life4 Defining another function that serves to count the number of times necessary to roll thedie and get all sides5 We will make a list with 7 elements. Index only indexes 1-6 will be used, so as to not confuse index 0 with a face of a die6 Initializes the variable to count how many rolls are required7 Creates a loop that will keep the simulation running until all sides of the die have been seen at least once8 Adds to the number of times a certain face of the die has been seen9-10 Keeps track of the number of times it took to roll the die, and returns the value to be used later11 Defines the main function that will compute the average number of rolls needed12 Initializes the variable that will count the total rolls needed when multiple trials are conducted13 Creates a loop that will run the experiment the number of times indicated in the range14 Calls the function that represents a single trial of finding the number of rolls needed15 Divides the total by the number of times the experiment is run in order to find the average16 Runs the entire


View Full Document

TAMU CSCE 110 - Simulations

Type: Lecture Note
Pages: 2
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Simulations
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Simulations and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Simulations 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?