1

I was wondering if someone can clarify this line for me.

Create a function die(x) which rolls a die x times keeping track of how many times each face comes up and returns a 1X6 array containing these numbers.

I am not sure what this means when it says 1X6 array ? I am using the randint function from numpy so the output is already an array (or list) im not sure.

Thanks

1
  • 2
    If this is homework, you should tag it as such. Commented Feb 27, 2011 at 22:41

5 Answers 5

3
def die(x):
    return np.bincount(np.random.random_integers(0, 5, size=x))

np.random.random_integers(0,5,size=x) rolls the die x times (faces are represented by numbers from 0 to 5 including).

np.bincount() returns the number of occurrences of each value in the array i.e., how many times each face comes up.

Example

>>> a = np.random.random_integers(0, 5, size=10)
>>> a
array([3, 5, 0, 5, 0, 5, 5, 1, 3, 0])
>>> np.bincount(a)
array([3, 1, 0, 2, 0, 4])
Sign up to request clarification or add additional context in comments.

Comments

3

Since a dice has 6 possible outcomes, if you get a 2 three times, this would be :

0 3 0 0 0 0

3 Comments

Thanks. So if my output is [1,5,5,2,3,4,6] (a list) how can I count the total lets say "2" values?
the array that you specify in your example is 1x7. But if it was [1,5,5,2,3,4], the "2" values would be 5.
Be careful not to confuse the array 0 index with a zero position. In programming terms, array[0] actually represents the "1"s, array[1] represents the "2"s and so on..
1

does this help?

def die(x):
    return [np.random.randint(1,6) for i in range(x)]

1 Comment

Hi, This is what I currently have but my question is not to find the answer but to understand what exactly its asking for. Using what you did before is what I did, I have a list of all the random values but im not sure what to do from there. Is my function returning the list of all the x outcomes or a total count for all the 1s, 2s, 3s in an array.. If the second one... how can I count the individual values from the list?
1

if you have the results of the die rolls in a list lst, you can determine the number of times a 4 appeared by doing len([_ for _ in lst if _ == 4]). you should be able to figure the rest out from there.

Comments

1
>>> myarray = [0]*6
>>> roll = 2
>>> myarray[roll-1]+=1
>>> myarray
[0, 1, 0, 0, 0, 0]
>>> myarray[roll-1]+=1
>>> myarray
[0, 2, 0, 0, 0, 0]
>>> roll = 6
>>> myarray[roll-1]+=1
>>> myarray
[0, 2, 0, 0, 0, 1]

now you just need to set roll from randint() or similar

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.