0

Related to this question I recently posted & resolved.

If the 3rd column of a matrix is to be changed to be the running total of the sums, how would I adjust my code to do so? This is where I'm at so far:

def dice(n):
    rolls = np.empty(shape=(n, 3),dtype=int)
    for i in range(n):
        x = random.randint(1,6)
        y = random.randint(1,6)
        if x in [1,3,5] or y in [1,3,5]:      
            sum_2_dice = x + y 
            z = np.cumsum(sum_2_dice)
            rolls[i,:] = x, y, z
        else:
            sum_2_dice = -(x+y)  # meaning you "lose the sum" basically
            z = np.cumsum(sum_2_dice)
            rolls[i,:] = x, y, z
    return rolls `

So for example: dice(2)

returns

array[[2, 6, -8],
      [1, 3,  4],
      [5, 2,  7])

when it should really be returning:

array[[2, 6, -8],
      [1, 3, -4],
      [5, 2,  3])

I thought np.cumsum would be doing something, but I'm not sure. Is a while loop needed to do this (I'm not sure of where it'd be applied)? I've tried various adjustments like instead of having z = np.cumsum(sum_2_dice) I did sum_2_dice += sum_2_dice (consequently the code that followed it was rolls[i,:] = x, y, sum_2_dice but that was terribly wrong since all that ended up doing was doubling the sum values in every column, not doing any sort of running total calculations.

glibdud
  • 7,550
  • 4
  • 27
  • 37
zainy
  • 111
  • 1
  • 1
  • 7
  • `x=1` and `y=3` satisfies the condition `if x in [1,3,5] or y in [1,3,5]:` therefore the `else` doesn't execute and the result is positive. Why do you think `x=1` and `y=3` should be returning a negative result? – DavidG Nov 10 '17 at 14:49
  • Ok so in the first row, `x=2` and `y=6` DOES NOT satisfy the if statement, and therefore returns a negative value, `-8`. Then the second row, you're right, it satisfies the if statement and would yield a positive value, `4`. However, since we're doing running totals in that column, we'd be calculating `-8 + 4` which returns `-4` ..... then in the third row, which also satisfies the if statement and would yield a positive value, `7`, we'd be doing `-4 + 7` in the third column of that row, which would return `3`. If that makes sense. – zainy Nov 10 '17 at 14:56
  • Ah I understand what you mean. You could just initialise `z` outside of the loop and keep adding the results of `sum_2_dice` – DavidG Nov 10 '17 at 15:02

1 Answers1

0

For your purposes, an easy way to keep track of z would be to initialise it outside of the loop, then keep adding the value of sum_2_dice.

def dice(n):
    z = 0
    rolls = np.empty(shape=(n, 3),dtype=int)
    for i in range(n):
        x = random.randint(1,6)
        y = random.randint(1,6)
        if x in [1,3,5] or y in [1,3,5]:
            sum_2_dice = x + y
            z += sum_2_dice
            rolls[i,:] = x, y, z
        else:
            sum_2_dice = -(x+y)  # meaning you "lose the sum" basically
            z += sum_2_dice
            rolls[i,:] = x, y, z
    return rolls


print (dice(3))
#[[ 6  2 -8]
# [ 4  5  1]
# [ 1  5  7]]

For reference, numpy.cumsum is normally used to get the cumulative sum of the elements of arrays, for example:

test = np.arange(0,5,1)
print (test)
# [0 1 2 3 4]

print (np.cumsum(test))
# [0 1 3 6 10]
DavidG
  • 24,279
  • 14
  • 89
  • 82