Python 3.6 Dice Histogram: Using random.seed effectively
up vote
1
down vote
favorite
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])
python python-3.x homework dice
add a comment |
up vote
1
down vote
favorite
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])
python python-3.x homework dice
4
"for this assignment" What is the assignment description?
– Mast
2 days ago
Apologies, I meant for a school assignment. I will edit the post to include the assignment description.
– mcinnis_k
yesterday
Much better, thanks!
– Mast
yesterday
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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])
python python-3.x homework dice
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])
python python-3.x homework dice
python python-3.x homework dice
edited yesterday
asked 2 days ago
mcinnis_k
264
264
4
"for this assignment" What is the assignment description?
– Mast
2 days ago
Apologies, I meant for a school assignment. I will edit the post to include the assignment description.
– mcinnis_k
yesterday
Much better, thanks!
– Mast
yesterday
add a comment |
4
"for this assignment" What is the assignment description?
– Mast
2 days ago
Apologies, I meant for a school assignment. I will edit the post to include the assignment description.
– mcinnis_k
yesterday
Much better, thanks!
– Mast
yesterday
4
4
"for this assignment" What is the assignment description?
– Mast
2 days ago
"for this assignment" What is the assignment description?
– Mast
2 days ago
Apologies, I meant for a school assignment. I will edit the post to include the assignment description.
– mcinnis_k
yesterday
Apologies, I meant for a school assignment. I will edit the post to include the assignment description.
– mcinnis_k
yesterday
Much better, thanks!
– Mast
yesterday
Much better, thanks!
– Mast
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
6
down vote
Various small comments:
- you can write
a[x+y] += 1
- you always call
random.seed
with the same input so you'll always have the same output. You could call it with no value so that it uses the time (which is different from one run to another). - instead of having an explicit 13 at the end of your code, you could use
len(a)
- you could use a constant to store
100
to avoid having a magic number in the middle of your code - you could use a constant to store the number of faces per dice
- you could initialise
a
by using list multiplication. In particular, you could reuse the constant introduced just before.
At this stage, you have:
NB_ITER = 100
DICE_MAX_NUM = 6
random.seed()
# declaring an array to count the occurrences
a= [0] * (2 * DICE_MAX_NUM + 1)
# looping and saving the count in array
for i in range(NB_ITER):
val = random.randint(1, DICE_MAX_NUM) + random.randint(1, DICE_MAX_NUM)
a[val] += 1
# printing histogram
for i in range(2, len(a)):
print(str(i).rjust(2) + "s: ", "*" * a[i])
To be continued but you could use collections.Counter...
2
+1 on theCounter
– Maarten Fabré
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Various small comments:
- you can write
a[x+y] += 1
- you always call
random.seed
with the same input so you'll always have the same output. You could call it with no value so that it uses the time (which is different from one run to another). - instead of having an explicit 13 at the end of your code, you could use
len(a)
- you could use a constant to store
100
to avoid having a magic number in the middle of your code - you could use a constant to store the number of faces per dice
- you could initialise
a
by using list multiplication. In particular, you could reuse the constant introduced just before.
At this stage, you have:
NB_ITER = 100
DICE_MAX_NUM = 6
random.seed()
# declaring an array to count the occurrences
a= [0] * (2 * DICE_MAX_NUM + 1)
# looping and saving the count in array
for i in range(NB_ITER):
val = random.randint(1, DICE_MAX_NUM) + random.randint(1, DICE_MAX_NUM)
a[val] += 1
# printing histogram
for i in range(2, len(a)):
print(str(i).rjust(2) + "s: ", "*" * a[i])
To be continued but you could use collections.Counter...
2
+1 on theCounter
– Maarten Fabré
yesterday
add a comment |
up vote
6
down vote
Various small comments:
- you can write
a[x+y] += 1
- you always call
random.seed
with the same input so you'll always have the same output. You could call it with no value so that it uses the time (which is different from one run to another). - instead of having an explicit 13 at the end of your code, you could use
len(a)
- you could use a constant to store
100
to avoid having a magic number in the middle of your code - you could use a constant to store the number of faces per dice
- you could initialise
a
by using list multiplication. In particular, you could reuse the constant introduced just before.
At this stage, you have:
NB_ITER = 100
DICE_MAX_NUM = 6
random.seed()
# declaring an array to count the occurrences
a= [0] * (2 * DICE_MAX_NUM + 1)
# looping and saving the count in array
for i in range(NB_ITER):
val = random.randint(1, DICE_MAX_NUM) + random.randint(1, DICE_MAX_NUM)
a[val] += 1
# printing histogram
for i in range(2, len(a)):
print(str(i).rjust(2) + "s: ", "*" * a[i])
To be continued but you could use collections.Counter...
2
+1 on theCounter
– Maarten Fabré
yesterday
add a comment |
up vote
6
down vote
up vote
6
down vote
Various small comments:
- you can write
a[x+y] += 1
- you always call
random.seed
with the same input so you'll always have the same output. You could call it with no value so that it uses the time (which is different from one run to another). - instead of having an explicit 13 at the end of your code, you could use
len(a)
- you could use a constant to store
100
to avoid having a magic number in the middle of your code - you could use a constant to store the number of faces per dice
- you could initialise
a
by using list multiplication. In particular, you could reuse the constant introduced just before.
At this stage, you have:
NB_ITER = 100
DICE_MAX_NUM = 6
random.seed()
# declaring an array to count the occurrences
a= [0] * (2 * DICE_MAX_NUM + 1)
# looping and saving the count in array
for i in range(NB_ITER):
val = random.randint(1, DICE_MAX_NUM) + random.randint(1, DICE_MAX_NUM)
a[val] += 1
# printing histogram
for i in range(2, len(a)):
print(str(i).rjust(2) + "s: ", "*" * a[i])
To be continued but you could use collections.Counter...
Various small comments:
- you can write
a[x+y] += 1
- you always call
random.seed
with the same input so you'll always have the same output. You could call it with no value so that it uses the time (which is different from one run to another). - instead of having an explicit 13 at the end of your code, you could use
len(a)
- you could use a constant to store
100
to avoid having a magic number in the middle of your code - you could use a constant to store the number of faces per dice
- you could initialise
a
by using list multiplication. In particular, you could reuse the constant introduced just before.
At this stage, you have:
NB_ITER = 100
DICE_MAX_NUM = 6
random.seed()
# declaring an array to count the occurrences
a= [0] * (2 * DICE_MAX_NUM + 1)
# looping and saving the count in array
for i in range(NB_ITER):
val = random.randint(1, DICE_MAX_NUM) + random.randint(1, DICE_MAX_NUM)
a[val] += 1
# printing histogram
for i in range(2, len(a)):
print(str(i).rjust(2) + "s: ", "*" * a[i])
To be continued but you could use collections.Counter...
answered 2 days ago
Josay
24.4k13782
24.4k13782
2
+1 on theCounter
– Maarten Fabré
yesterday
add a comment |
2
+1 on theCounter
– Maarten Fabré
yesterday
2
2
+1 on the
Counter
– Maarten Fabré
yesterday
+1 on the
Counter
– Maarten Fabré
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208057%2fpython-3-6-dice-histogram-using-random-seed-effectively%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
"for this assignment" What is the assignment description?
– Mast
2 days ago
Apologies, I meant for a school assignment. I will edit the post to include the assignment description.
– mcinnis_k
yesterday
Much better, thanks!
– Mast
yesterday