Home > Mobile >  How do I roll 4 dice 6 different times and have the output of all 6 iterations?
How do I roll 4 dice 6 different times and have the output of all 6 iterations?

Time:12-05

So what I'm trying to do is be able to roll 6 sets of 4 individual 6 sided dice. The current code that I currently have is:

import random


stat_rolls = []

for i in range(6):
    for j in range(4):
        num = random.randint(1, 6)
        stat_rolls.append(num)

print(stat_rolls)

Currently the output is as follows:

[5, 6, 5, 2, 5, 6, 2, 2, 4, 6, 3, 1, 3, 5, 5, 3, 5, 5, 6, 4, 3, 4, 1, 4]

What I actually want the output to look like is the following:

[5, 6, 5, 2]
[5, 6, 2, 2]
[4, 6, 3, 1]
[3, 5, 5, 3]
[5, 5, 6, 4]
[3, 4, 1, 4]

How would this be possible? Would it be through a dict function? I'm still learning Python so any kind of help would be much appreciated!!

CodePudding user response:

One really basic whay of achieving what you want is to create a buffer array that gets filled with a cycle of trows and then appending it to an upper array to get the desired format:

import random


stat_rolls = []
buffer=[]
for i in range(6):
    buffer=[]
    for j in range(4):
        num = random.randint(1, 6)
        buffer.append(num)
    stat_rolls.append(buffer)

print(stat_rolls)

this outputs:

[[4, 4, 6, 1], [1, 4, 2, 3], [3, 5, 3, 3], [3, 2, 5, 2], [6, 3, 5, 4], [3, 2, 5, 1]]

you can then change your print format to get to different print layouts,for example,if you print it out as:

for i in stat_rolls:
    print(i)

you get:

[6, 4, 1, 4]
[3, 5, 2, 1]
[5, 1, 5, 6]
[6, 4, 1, 3]
[4, 6, 6, 2]
[5, 2, 3, 3]

  • Related