暫無描述

different_dice.py 835B

12345678910111213141516171819202122232425262728293031323334
  1. from die import Die
  2. import pygal
  3. # Create a D6 and a D10.
  4. die_1 = Die()
  5. die_2 = Die(10)
  6. # Make some rolls, and store results in a list.
  7. results = []
  8. for roll_num in range(50000):
  9. result = die_1.roll() + die_2.roll()
  10. results.append(result)
  11. # Analyze the results.
  12. frequencies = []
  13. max_result = die_1.num_sides + die_2.num_sides
  14. for value in range(2, max_result+1):
  15. frequency = results.count(value)
  16. frequencies.append(frequency)
  17. # Visualize the results.
  18. hist = pygal.Bar()
  19. hist.force_uri_protocol = 'http'
  20. hist.title = "Results of rolling a D6 and a D10 50,000 times."
  21. hist.x_labels = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
  22. '13', '14', '15', '16']
  23. hist.x_title = "Result"
  24. hist.y_title = "Frequency of Result"
  25. hist.add('D10 + D10', frequencies)
  26. hist.render_to_file('dice_visual.svg')