Skip to main content
edited tags; edited title
Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

Python 3.6 Dice Histogram: Using random.seed effectively

Added more information regarding assignment
Source Link

I am not 100% sure that I am using the random.seed correctly for this assignment, or if there is a more efficient way to write the code. Any feedback is appreciated.

Assignment Description: Simulate rolling 2 die one hundred times. Keep track of the total of the two die. Print a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character, such as *, that number of times

import random
random.seed(2)
# declaring an array to count the occurrences
a= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

# looping for 100 times and saving the count in array
for i in range(100):
    x = random.randint(1,6)
    y = random.randint(1,6)
    a[x+y] = a[x+y] + 1

# printing histogram  
for i in range(2, 13):
    print(str(i).rjust(2)+"s: ","*"*a[i])

I am not 100% sure that I am using the random.seed correctly for this assignment, or if there is a more efficient way to write the code. Any feedback is appreciated.

import random
random.seed(2)
# declaring an array to count the occurrences
a= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

# looping for 100 times and saving the count in array
for i in range(100):
    x = random.randint(1,6)
    y = random.randint(1,6)
    a[x+y] = a[x+y] + 1

# printing histogram  
for i in range(2, 13):
    print(str(i).rjust(2)+"s: ","*"*a[i])

I am not 100% sure that I am using the random.seed correctly for this assignment, or if there is a more efficient way to write the code. Any feedback is appreciated.

Assignment Description: Simulate rolling 2 die one hundred times. Keep track of the total of the two die. Print a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character, such as *, that number of times

import random
random.seed(2)
# declaring an array to count the occurrences
a= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

# looping for 100 times and saving the count in array
for i in range(100):
    x = random.randint(1,6)
    y = random.randint(1,6)
    a[x+y] = a[x+y] + 1

# printing histogram  
for i in range(2, 13):
    print(str(i).rjust(2)+"s: ","*"*a[i])
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
Loading