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])









share|improve this question




















  • 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















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])









share|improve this question




















  • 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













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])









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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










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...






share|improve this answer

















  • 2




    +1 on the Counter
    – Maarten Fabré
    yesterday











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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...






share|improve this answer

















  • 2




    +1 on the Counter
    – Maarten Fabré
    yesterday















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...






share|improve this answer

















  • 2




    +1 on the Counter
    – Maarten Fabré
    yesterday













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...






share|improve this answer












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...







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Josay

24.4k13782




24.4k13782








  • 2




    +1 on the Counter
    – Maarten Fabré
    yesterday














  • 2




    +1 on the Counter
    – Maarten Fabré
    yesterday








2




2




+1 on the Counter
– Maarten Fabré
yesterday




+1 on the Counter
– Maarten Fabré
yesterday


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Ellipse (mathématiques)

Quarter-circle Tiles

Mont Emei