class DieRollTest(unittest.TestCase): """Test the functionality of the Die class roll function.""" def setUp(self): self.possible_values = [1, 2, 3, "Dog", "Cat", "Hippo"] self.new_die = Die(self.possible_values) # print(self.shortDescription()) def tearDown(self): del self.possible_values del self.new_die # print("Just ran test:") # print(self._testMethodName) def test_roll_once(self): """Roll die once, and ensure returned value is in possible_values.""" self.assertIn(self.new_die.roll(), self.possible_values, "Rolled value was not in possible values of Die.") def test_rolled_value_changes(self): """Roll die a number of times and make sure it changes its value between rolls.""" holding_value = self.new_die.roll() for i in range(10): if self.new_die.roll() != holding_value: print("Rolled die '{}' value is different " "than holding value '{}'." .format(self.new_die.current_value, holding_value)) self.assertTrue(True) return self.assertTrue(False, "Rolled die value did not change in 10 rolls.") def test_current_value_is_updated_to_rolled_value(self): """Roll die a number of times and make sure its value updates according to the rolled value.""" self.new_die.current_value = 9 self.assertEqual(self.new_die.roll(), self.new_die.current_value, "Die's value did not update to the rolled value.")
class DieRollTest(unittest.TestCase): """Test the functionality of the Die class roll function.""" def setUp(self): self.possible_values = [1, 2, 3, "Dog", "Cat", "Hippo"] self.new_die = Die(self.possible_values) # print(self.shortDescription()) def tearDown(self): del self.possible_values del self.new_die # print("Just ran test:") # print(self._testMethodName) def test_roll_once(self): """Roll die once, and ensure returned value is in possible_values.""" self.assertIn(self.new_die.roll(), self.possible_values, "Rolled value was not in possible values of Die.") def test_rolled_value_changes(self): """Roll die a number of times and make sure it changes its value between rolls.""" holding_value = self.new_die.roll() for i in range(10): if self.new_die.roll() != holding_value: print("Rolled die '{}' value is different " "than holding value '{}'.".format( self.new_die.current_value, holding_value)) self.assertTrue(True) return self.assertTrue(False, "Rolled die value did not change in 10 rolls.") def test_current_value_is_updated_to_rolled_value(self): """Roll die a number of times and make sure its value updates according to the rolled value.""" self.new_die.current_value = 9 self.assertEqual(self.new_die.roll(), self.new_die.current_value, "Die's value did not update to the rolled value.")
def setUp(self): self.possible_values = [1, 2, 3, "Dog", "Cat", "Hippo"] self.new_die = Die(self.possible_values)
import matplotlib.pyplot as plt plt.rcParams['font.family'] = 'Hiragino Sans' from die_class import Die '''サイコロを2個転がした時のシミュレーションをmatplotlibで行う。''' die_1 = Die() die_2 = Die() #出た目の結果を格納する箱。 results = [] for i in range(10000): result = die_1.roll() + die_2.roll() results.append(result) #ヒストグラムの作成。 plt.title('2個のサイコロを転がした時のヒストグラム') plt.xlabel('出た目の和') plt.ylabel('回数') plt.hist(results, bins=11) plt.show()
from die_class import Die from plotly.graph_objs import Bar, Layout from plotly import offline #Create a D6. die = Die() #Create a list to store roll results. results = [] #Make rolls and store results in results list. for roll_num in range(1000): result = die.roll() results.append(result) print(results) #Analyze results of rolls. #Create a list for frequencies. frequencies = [] for value in range(1, die.num_sides + 1): frequency = results.count(value) frequencies.append(frequency) print(frequencies) #Create a histogram. x_values = list(range(1, die.num_sides + 1)) data = [Bar(x=x_values, y=frequencies)]