Python homework
Terry Reedy
tjreedy at udel.edu
Wed Dec 13 18:18:08 EST 2017
On 12/13/2017 8:28 AM, boffi at choices.random.py wrote:
> nick.martinez2 at aol.com (nick martinez2) writes:
>
>> def rollDie(number):
>> rolls = [0] * 6
>> for i in range(0, number):
>> roll=int(random.randint(1,6))
One could just as well use randint(0, 5) and skip the -1 below.
>> rolls[roll - 1] += 1
>> return rolls
This returns a list with a count of how many times each of 1 to 6 is
chosen in *number* rolls.
> def rollDie(number):
> from random import choices
> return choices((1,2,3,4,5,6), k=number)
This returns a list with all *number* rolls. Assuming the the first
function is the one wanted, one could feed choices into a counter. (The
import should be outside of the function.)
def rollDie(number):
rolls = [0] * 6
for i in random.choices((0,1,2,3,4,5), k=number):
rolls[i] += 1
return rolls
--
Terry Jan Reedy
More information about the Python-list
mailing list