Main.Lab10 History
Hide minor edits - Show changes to markup
- 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.
- 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.
- The
Dieclass should have instance variables callednum_facesandroll_valuewhich store the number of faces and the current value rolled. The roll_value should be initialized toNonewhen an instance is first created. Your class should also have an instance of the classrandom.Randomthat is used for generating the rolls.
- The
Dieclass should have instance variables callednum_facesandroll_valuewhich store the number of faces and the current value rolled. The roll_value should be initialized toNonewhen an instance is first created.
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
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
Programming your own classes
Lab10 - Programming your own classes
- A
roll()method that rolls the die and returns the value that was rolled. The value is stored in theroll_valueattribute. To roll the die use therandrangemethod of the instance ofrandom.Randomyour class is storing.
- A
roll()method that rolls the die and returns the value that was rolled. The value is stored in theroll_valueattribute. To roll the die use therandom.randrangemethod. See below for details on using the Python random number generator.
Create a list of the result of rolling a 10-faced die 10000 times and compute the fraction of times each value was rolled.
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.
- 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
Dieinstance as: die = Die(num_faces = 4, seed = 10) - The
Dieclass should have instance variables callednum_facesandroll_valuewhich store the number of faces and the current and the current value rolled. The roll_value should be initialized toNonewhen an instance is first created. Your class should also have an instance of the classrandom.Randomthat is used for generating the rolls.
- 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
Dieclass should have instance variables callednum_facesandroll_valuewhich store the number of faces and the current value rolled. The roll_value should be initialized toNonewhen an instance is first created. Your class should also have an instance of the classrandom.Randomthat is used for generating the rolls.
Write a class called Die that can simulate the rolling of a single Die. Your class should have the following methods:
Write a class called Die that can simulate the rolling of a single die. Your class should have the following methods and attributes:
- 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
Nonewhen an instance is first created. Your class should also have an instance of the classrandom.Random.
- The
Dieclass should have instance variables callednum_facesandroll_valuewhich store the number of faces and the current and the current value rolled. The roll_value should be initialized toNonewhen an instance is first created. Your class should also have an instance of the classrandom.Randomthat is used for generating the rolls.
Create a list of the result of rolling a faced die 1000 times and compute the fraction of times each value was rolled.
Create a list of the result of rolling a 10-faced die 10000 times and compute the fraction of times each value was rolled.
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
Dieinstance 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
Nonewhen an instance is first created. Your class should also have an instance of the classrandom.Random. - A
roll()method that rolls the die and returns the value that was rolled. The value is stored in theroll_valueattribute. To roll the die use therandrangemethod of the instance ofrandom.Randomyour 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.
