def show_one_die_bar(numbers, num_sides=6): """投掷骰子numbers次,看每个面出现的次数,用直方图显示结果""" die = Die(num_sides) results = [] title_num_sides = "D" + str(num_sides) # 投掷骰子,把结果保存在results中 for number in range(numbers): results.append(die.roll()) # 分析结果 frequencies = [] for value in range(1, die.num_sides+1): frequencies.append(results.count(value)) # 显示结果在Console窗口 #print(results) #print(frequencies) # 对结果可视化 hist = Bar() hist.title = "Results of rolling one " + title_num_sides + " " + str(numbers) + " times" hist.x_labels = [str(num) for num in range(1,num_sides + 1)] hist.x_title = "Result" hist.y_title = "Frequency of Result" hist.add(title_num_sides, frequencies) hist.render_to_file("die_visual_" + title_num_sides + "_" + str(numbers) + ".svg")
def main(): # Set the dice up numSides = 20 die1 = Die(numSides) die2 = Die(numSides) numRolls = 100000 # Roll the die numRoll times and store the sum of the two. results = [] for roll in range(numRolls): result = die1.roll() + die2.roll() results.append(result) # Anaylizing the results from above. frequencies = [] maxResult = die1.numSides + die2.numSides for value in range(2, maxResult + 1): frequency = results.count(value) frequencies.append(frequency) # Create a graph from the results. graph = Bar() graph.title = f'Results of rolling two D{numSides} die {numRolls} times.' # Create the x labels graph.x_labels = [] for x in range(2, maxResult + 1): graph.x_labels.append(f'{x}') # Add axii titles. graph.x_title = 'Result' graph.y_title = 'Frequency of Result' graph.add(f'D{numSides} + D{numSides}', frequencies) # Add teh frequencies to the graph # Get the script directory and add the file name to it. filePath = f'{path.dirname(__file__)}/d{numSides}_dice_simulation.svg' graph.render_to_file(filePath) # Create the visual file.
def show_two_die_bar(numbers, num_sides1=6, num_sides2=6): """投掷骰子numbers次,看每个面出现的次数,用直方图显示结果""" die1 = Die(num_sides1) title_num_sides1 = "D" + str(num_sides1) die2 = Die(num_sides2) title_num_sides2 = "D" + str(num_sides2) results = [] # 投掷两个骰子,把相加结果保存在results中 for number in range(numbers): results.append(die1.roll() + die2.roll()) # 分析结果 max_result = num_sides1 + num_sides2 min_result = 2 frequencies = [] for value in range(min_result, max_result+1): frequencies.append(results.count(value)) # 显示结果在Console窗口 #print(results) #print(frequencies) # 对结果可视化 hist = Bar() if title_num_sides1 == title_num_sides2: hist.title = ("Results of rolling two " + title_num_sides1 + " " + str(numbers) + " times") else: hist.title = ("Results of rolling one " + title_num_sides1 + " and one " + title_num_sides2 + " " + str(numbers) + " times") hist.x_labels = [str(num) for num in range(min_result, max_result+1)] hist.x_title = "Result" hist.y_title = "Frequency of Result" hist.add(title_num_sides1 + " + " + title_num_sides2, frequencies) hist.render_to_file("die_visual_" + title_num_sides1 + "_" + title_num_sides2 + "_" + str(numbers) + ".svg")
else: roll = 2000 print( 'Input was not provided or invalid, so the default value of 1,000 will be used.' ) L = sorted(L) result = [] for i in range(roll): sums = 0 for j in range(len(L)): sums += randint(1, L[j]) result.append(sums) range_of_sums = list(set(result)) counts = [result.count(i) for i in range_of_sums] counts = [{ 'value': count, 'label': f'Frequency: {count / roll:.2f}' } for count in counts] histogram = Bar(style=Style(colors=('#228B22', ), major_label_font_size=12), show_legend=False) histogram.title = f'Simulation for {roll} rolls of the dice: {sorted(L)}' histogram.x_labels = [str(i) for i in range_of_sums] histogram.x_title = 'Possible sums' histogram.y_title = 'Counts' histogram.add('', counts) histogram.render_to_file('dice_rolls.svg')
die_1 = Die() die_2 = Die(10) #Roll the dice some times and stores it in a list results = [] for roll_num in range(50000): result = die_1.roll() + die_2.roll() results.append(result) #Analise the results max_result = die_1.sides + die_2.sides frequencies = [] for value in range(2, max_result + 1): frequency = results.count(value) frequencies.append(frequency) hist = Bar() hist.title = 'Result of rolling a D6 and a D10 50.000 times' hist.x_labels = [str(i) for i in range(2, max_result + 1)] hist.x_title = 'Result' hist.y_title = 'Frequency of Result' hist.add('D6 + D10', frequencies) hist.render_to_file('different_dice_visual.svg') sum = 0 for frequency in frequencies: sum += frequency print(sum)
from die import Die from pygal import Bar #Creates two D6 dice die_1 = Die() die_2 = Die() times = 1000 #Roll the dice one thousand times results = [die_1.roll() + die_2.roll() for _ in range(times)] #Analize the results max_result = die_1.sides + die_2.sides frequencies = [results.count(value) for value in range(2, max_result + 1)] #Visualize the results hist = Bar() hist.title = 'Results of rolling a two D5 dice ' + str(times) + ' times' hist.x_labels = [str(i) for i in range(2, 13)] hist.x_title = 'Sum of Sides' hist.y_title = 'Frequency of Sum' hist.add('D6 + D6', frequencies) hist.render_to_file('dice_visual.svg')
from die import Die from pygal import Bar #Creates two D6 dice (D6 == die of 6 sides) die = Die() times = 1000 #Roll the die and store the results in a list results = [die.roll() for roll_num in range(times)] #Analize the results frequencies = [results.count(value) for value in range(1, die.sides + 1)] #Visualizes the results hist = Bar() hist.title = 'Results of rolling one D6 die ' + str(times) + ' times' hist.x_labels = [str(i) for i in range(1, 7)] hist.x_title = 'Sides' hist.y_title = 'Frequency of Side' #add will add automatically the y_labels hist.add('D6', frequencies) hist.render_to_file('die_visual.svg')
from die import Die from pygal import Bar die_1 = Die() # 十面骰子 die_2 = Die(10) results = [] frequencies = [] for roll in range(10000): result = die_1.roll() + die_2.roll() results.append(result) max_result = die_1.num_sides + die_2.num_sides for value in range(2, max_result + 1): frequency = results.count(value) frequencies.append(frequency) hist = Bar() hist.title = "Results of rolling a D6 and a D10 10000 times." # 随着数据的增大,最好利用循环生成x坐标列表 hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'] hist.x_title = "Result" hist.y_title = "Frequency of Results" hist.add("D6+D10", frequencies) hist.render_to_file("die_simulation_pro.svg")
from throw_dice.die import Die from pygal import Bar # создание кубиков die_1 = Die(8) die_2 = Die(8) # моделирование серии бросков с сохранением данных max_throws = 10000 results = [die_1.roll() + die_2.roll() for roll_num in range(max_throws)] print(results) # анализ результатов max_result = die_1.num_sides + die_2.num_sides frequencies = [results.count(value) for value in range(2, max_result + 1)] print(frequencies) # визуализация результатов histogram = Bar() histogram.title = f"Результаты брасания двух кубиков {max_throws} раз" histogram.x_labels = [num for num in range(2, max_result + 1)] histogram.x_title = "Сумма выпадений" histogram.y_title = "Количество выпадений" histogram.add("Сумма граней кубиков", frequencies) histogram.render_to_file('dice_visual.svg')
xlab = [] items = [] begin = lb end = ub while count[begin] == 0: begin += 1 while count[end] == 0: end -= 1 for i in range(begin, end): xlab.append(i) dic = {"value": count[i], "label": f"Frequency: {count[i] / n:.2f}"} items.append(dic) # # Render barch = Bar() barch.title = f"Simulation for {n} rolls of the dice: {dice}" barch.x_labels = xlab barch.x_title = "Possible sums" barch.y_title = "Counts" barch.show_legend = False barch.force_uri_protocol = "http" barch.add('', items) barch.style = Style(colors=("#228B22",), label_font_size=12) barch.render() barch.render_to_file("dice_rolls.svg") # # Open browser window open_new_tab("file://" + path.realpath("dice_rolls.svg"))
"""Determine a value based on its percentage.""" p_index = int(p * len(x)) return str(sorted(x)[p_index]) ############## # Dispersion # ############## def data_range(x): return str(max(x) - min(x)) # Execute tests and save the results. with open('data.txt', 'w') as f: f.write('Two D6 Analysis') f.write('\nMean result: ' + mean(results)) f.write('\nMedian result: ' + median(results)) f.write('\nQuantile result: ' + quantile(results, .90)) f.write('\nData Range: ' + data_range(results)) # Visualize the results. hist = Bar() hist.title = 'Results of rolling 2d6 1,000,000 times.' hist.x_labels = [i for i in range(2, die1.num_sides * 2 + 1)] hist.x_title = 'Result' hist.y_title = 'Frequency of Result' hist.add('2d6', frequencies) hist.render_to_file('test.svg')