Main.Lab10 History

Hide minor edits - Show changes to markup

April 11, 2013, at 04:23 PM MST by 129.82.47.51 -
Changed line 9 from:
  • A constructor that takes two parameters: num_faces: the number of faces of the die (default should be six, and your code should check that when this is provided, it is an integer whose value is greater or equal to 4) and seed: the seed for initializing the random number generator. The default is having no seed. num_faces - the number of faces in the die.
to:
  • A constructor that takes two parameters: num_faces: the number of faces of the die (default should be six, and your code should check that when this is provided, it is an integer whose value is greater or equal to 4) and seed: the seed for initializing the random number generator. The default is having no seed.
April 11, 2013, at 04:22 PM MST by 129.82.47.51 -
Changed line 11 from:
  • The Die class should have instance variables called num_faces and roll_value which store the number of faces and the current value rolled. The roll_value should be initialized to None when an instance is first created. Your class should also have an instance of the class random.Random that is used for generating the rolls.
to:
  • The Die class should have instance variables called num_faces and roll_value which store the number of faces and the current value rolled. The roll_value should be initialized to None when an instance is first created.
April 10, 2013, at 11:28 AM MST by 129.82.44.223 -
Changed lines 24-25 from:

random.seed(10) # or random.seed(None) if you don't want to set a seed print random.randrange(1, 7) # generate a random number between 1 and 6

to:

random.seed(10)

  1. or random.seed(None) if you don't want to set a seed

print random.randrange(1, 7)

  1. generate a random number between 1 and 6
April 10, 2013, at 11:27 AM MST by 129.82.44.223 -
Changed lines 1-2 from:

Programming your own classes

to:

Lab10 - Programming your own classes

Changed lines 13-14 from:
  • A roll() method that rolls the die and returns the value that was rolled. The value is stored in the roll_value attribute. To roll the die use the randrange method of the instance of random.Random your class is storing.
to:
  • A roll() method that rolls the die and returns the value that was rolled. The value is stored in the roll_value attribute. To roll the die use the random.randrange method. See below for details on using the Python random number generator.
Changed lines 17-27 from:

Create a list of the result of rolling a 10-faced die 10000 times and compute the fraction of times each value was rolled.

to:

Create a list of the result of rolling a 10-faced die 10000 times and compute the fraction of times each value was rolled.

Random numbers in Python

Here are a few Python commands that will need for implementing your Die class: (:source lang=python:) import random random.seed(10) # or random.seed(None) if you don't want to set a seed print random.randrange(1, 7) # generate a random number between 1 and 6 (:sourceend:) More details on the Python random number generator can be found in the library documentation.

April 10, 2013, at 11:17 AM MST by 129.82.44.223 -
Changed lines 9-11 from:
  • A constructor that takes two keyword parameters: seed - the seed for initializing the random number generator. The default is having no seed; num_faces - the number of faces in the die. The default is 6, and your code should check that when this is provided, it is an integer whose value is greater or equal to 4. So the user should be able to create a Die instance as: die = Die(num_faces = 4, seed = 10)
  • The Die class should have instance variables called num_faces and roll_value which store the number of faces and the current and the current value rolled. The roll_value should be initialized to None when an instance is first created. Your class should also have an instance of the class random.Random that is used for generating the rolls.
to:
  • A constructor that takes two parameters: num_faces: the number of faces of the die (default should be six, and your code should check that when this is provided, it is an integer whose value is greater or equal to 4) and seed: the seed for initializing the random number generator. The default is having no seed. num_faces - the number of faces in the die.
  • The Die class should have instance variables called num_faces and roll_value which store the number of faces and the current value rolled. The roll_value should be initialized to None when an instance is first created. Your class should also have an instance of the class random.Random that is used for generating the rolls.
April 05, 2010, at 11:21 AM MST by 10.84.44.76 -
Changed lines 7-8 from:

Write a class called Die that can simulate the rolling of a single Die. Your class should have the following methods:

to:

Write a class called Die that can simulate the rolling of a single die. Your class should have the following methods and attributes:

Changed line 11 from:
  • The die class should have instance variables called num_faces and roll_value which store the number of faces and the current and the current value rolled. The roll_value should be initialized to None when an instance is first created. Your class should also have an instance of the class random.Random.
to:
  • The Die class should have instance variables called num_faces and roll_value which store the number of faces and the current and the current value rolled. The roll_value should be initialized to None when an instance is first created. Your class should also have an instance of the class random.Random that is used for generating the rolls.
April 05, 2010, at 11:19 AM MST by 10.84.44.76 -
Changed line 17 from:

Create a list of the result of rolling a faced die 1000 times and compute the fraction of times each value was rolled.

to:

Create a list of the result of rolling a 10-faced die 10000 times and compute the fraction of times each value was rolled.

April 05, 2010, at 11:04 AM MST by 10.84.44.76 -
Added lines 1-17:

Programming your own classes

In this lab we will get some practice in writing classes and using Python's random number generator.

The Die class

Write a class called Die that can simulate the rolling of a single Die. Your class should have the following methods:

  • A constructor that takes two keyword parameters: seed - the seed for initializing the random number generator. The default is having no seed; num_faces - the number of faces in the die. The default is 6, and your code should check that when this is provided, it is an integer whose value is greater or equal to 4. So the user should be able to create a Die instance as: die = Die(num_faces = 4, seed = 10)
  • The die class should have instance variables called num_faces and roll_value which store the number of faces and the current and the current value rolled. The roll_value should be initialized to None when an instance is first created. Your class should also have an instance of the class random.Random.
  • A roll() method that rolls the die and returns the value that was rolled. The value is stored in the roll_value attribute. To roll the die use the randrange method of the instance of random.Random your class is storing.
  • A __repr__() method that returns a useful string representation of the current state of the object.

Create a list of the result of rolling a faced die 1000 times and compute the fraction of times each value was rolled.