Home > OS >  trying to write a function that find the combined result of n many 6 sided die
trying to write a function that find the combined result of n many 6 sided die

Time:11-01

trying to create a function that will add the sum when n number of dies are rolled once. I have this so far but get a name error saying n is not defined any advice would be helpful

    import numpy as np
    def dice_rolln(n):
      np.random.seed(0)
      x = [np.random.randint(1, 7) for _ in range(n)]
      return sum[x]

CodePudding user response:

Your function has two problems:

  1. You're seeding the RNG the same way each time, which means you'll get the same results each time.
  2. Your syntax for calling the sum function is incorrect. Functions are called with (), not [].

Here's a simple implementation using random.randint:

>>> import random
>>> def roll_nd6(n: int) -> int:
...     return sum(random.randint(1, 6) for _ in range(n))
...
>>> roll_nd6(1)
5
>>> roll_nd6(10)
40
>>> roll_nd6(10)
34

CodePudding user response:

try this

import numpy as np
def dice_rolln(n):
      np.random.seed(0)
      x = [np.random.randint(1, 7) for _ in range(n)]
      return sum(x)

is you looking for this??

  • Related