How to make a sample space using 4.6 and 8-sided data?

Asked

Viewed 397 times

3

I am doing a 3-data sample space probability work (one on 4 sides, one on 6 sides and the other on 8 sides)
I know that the sample space, without repetition between the data, is 4*6*8 = 192.
I saw in a theme here the following script:

from itertools import product

caracteres = [0, 1, 2]
permsList = []
genComb = product(caracteres, repeat=2) # aqui e onde tens de especificar o numero de chars que cada combinacao tenha
for subset in genComb:
    print(subset) # tuple retornado com uma combinacao por loop
    permsList.append(subset)
print(permsList) # [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

Where the product command generates all possible combinations, including repeated characters

But only two things were missing:

  1. possible combinations not to be repeated
  2. the data were from 1 to their faces.

For example:

[(4,6,8); (3,6,8) ; (4,5,8) ; (4,6,7) ; (2,6,8) ; . . . ]

Someone could help me?

Edit (solved) I wrote a scrip, with the help of those involved here, and you can make a sample space for as many data as you want, up to 5, with as many desired faces in each:

from itertools import product

if(A==1 or A==2 or A==3 or A==4 or A==5):
    if(A==1):
        a=input("Quantas faces possui o dado 1?: ")

        print(list(product(range(1, a+1))))

    if(A==2):
        a=int(input("Quantas faces possui o dado 1?: "))
        b=int(input("Quantas faces possui o dado 2?: "))

        print(list(product(range(1, a+1), range(1, b+1))))

    if(A==3):
        a=int(input("Quantas faces possui o dado 1?: "))
        b=int(input("Quantas faces possui o dado 2?: "))
        c=int(input("Quantas faces possui o dado 3?: "))

        print(list(product(range(1, a+1), range(1, b+1), range(1, c+1))))

    if(A==4):
        a=int(input("Quantas faces possui o dado 1?: "))
        b=int(input("Quantas faces possui o dado 2?: "))
        c=int(input("Quantas faces possui o dado 3?: "))
        d=int(input("Quantas faces possui o dado 4?: "))

        print(list(product(range(1, a+1), range(1, b+1), range(1, c+1), range(1, d+1))))

    if(A==5):
        a=int(input("Quantas faces possui o dado 1?: "))
        b=int(input("Quantas faces possui o dado 2?: "))
        c=int(input("Quantas faces possui o dado 3?: "))
        d=int(input("Quantas faces possui o dado 4?: "))
        e=int(input("Quantas faces possui o dado 5?: "))

        print(list(product(range(1, a+1), range(1, b+1), range(1, c+1), range(1, d+1), range(1, e+1))))


else:
    print("Utilize números inteiros de 1 a 5")

In case you want to know the event:

from itertools import product

A=int(input("Quantas dados serão utilizados? (1-5): "))


if(A==1 or A==2 or A==3 or A==4 or A==5):
    if(A==1):
        a=input("Quantas faces possui o dado 1?: ")
        X=input("Qual o evento (soma do número de faces)?: ")

        resultado = product(range(1, a+1))
        somam = [ x for x in resultado if x[0] == X]
        print("Os eventos são, para os dados de :")
        print(somam)


    if(A==2):
        a=int(input("Quantas faces possui o dado 1?: "))
        b=int(input("Quantas faces possui o dado 2?: "))
        X=input("Qual o evento (soma do número de faces)?: ")

        resultado = product(range(1, a+1), range(1,b+1))
        somam = [ x for x in resultado if x[0] + x[1] == X]
        print("Os eventos são, para os dados de :")
        print(somam)

    if(A==3):
        a=int(input("Quantas faces possui o dado 1?: "))
        b=int(input("Quantas faces possui o dado 2?: "))
        c=int(input("Quantas faces possui o dado 3?: "))
        X=input("Qual o evento (soma do número de faces)?: ")

        resultado = product(range(1, a+1), range(1,b+1), range(1,c+1))
        somam = [ x for x in resultado if x[0] + x[1] + x[2] == X]
        print("Os eventos são, para os dados de :")
        print(somam)


    if(A==4):
        a=int(input("Quantas faces possui o dado 1?: "))
        b=int(input("Quantas faces possui o dado 2?: "))
        c=int(input("Quantas faces possui o dado 3?: "))
        d=int(input("Quantas faces possui o dado 4?: "))
        X=input("Qual o evento (soma do número de faces)?: ")

        resultado = product(range(1, a+1), range(1,b+1), range(1,c+1), range(1,d+1))
        somam = [ x for x in resultado if x[0] + x[1] + x[2] + x[3] == X]
        print("Os eventos são, para os dados de :")
        print(somam)

    if(A==5):
        a=int(input("Quantas faces possui o dado 1?: "))
        b=int(input("Quantas faces possui o dado 2?: "))
        c=int(input("Quantas faces possui o dado 3?: "))
        d=int(input("Quantas faces possui o dado 4?: "))
        e=int(input("Quantas faces possui o dado 5?: "))
        X=input("Qual o evento (soma do número de faces)?: ")

        resultado = product(range(1, a+1), range(1,b+1), range(1,c+1), range(1,d+1), range(1,e+1))
        somam = [ x for x in resultado if x[0] + x[1] + x[2] + x[3] + x[4] == X]
        print("Os eventos são, para os dados de :")
        print(somam)
else:
    print("Utilize números inteiros de 1 a 5")   
  • 1

    Let’s make this clear: the four-sided die has the sides [1, 2, 3, 4], the one of six has [1, 2, 3, 4, 5, 6] and the eight has [1, 2, 3, 4, 5, 6, 7, 8], correct? Do you want to generate all combinations where the value of the sides does not repeat? For example, the output must contain [1, 2, 3], but may not contain [1, 2, 1], that’s it?

  • @Andersoncarloswoss besides the repetition of values, it is necessary to know if the order matters, e.g. if [1, 2, 3] == [3, 2, 1] will be considered True or not

  • @Very well observed blogger. Taking advantage, I calculated here and 192 is the number of sequences with repetition. No repetition, as requested, has only 120 sequences.

  • I must have expressed myself wrong. So it is: the entire sample space is 192, with non-repeats being, for example, [(4,6,8); (3,6,8) ; (4,5,8) ; (4,6,8); (4,5,8); . . . ], is not repeating previous values. The values obtained may be [(1,1,1); (1,1,2); (1,2,1); (1,3,2); ... ] indefinitely, only if the order is prevailed, that is, in this way: whereas a, b and c belong to the positive integers only, the following order [(a,b,c)], cannot be [(b,a,c)], i.e., not changing order

  • @Andersoncarloswoss, is that I want the possible combinations in the release of these three dice, ie the sample space. Replying to your question, the order does not matter, for your example, [1, 2, 3] is different from [3, 2, 1], would be two possible combinations here In the case of my previous comment, 'a' would assume values between 1 and 4 (given from 4 sides), 'b' asumiria values from 1 to 6 (data from 6 sides) and 'c' would assume values from 1 to 8 sides (data from 8 sides)

1 answer

4


If what you seek is to get the combination of the results of the three data (all 192), a simple for loop would suffice:

lista = []
for i in range(1, 5):
    for j in range(1, 7):
        for k in range(1, 9):
            lista.append((i, j, k))

Or, using the itertools as in your example:

from itertools import product
list(product(range(1, 5), range(1, 7), range(1, 9)))

In both examples, the result is the following:

[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 1, 6),
 (1, 1, 7), (1, 1, 8), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 2, 4),
 (1, 2, 5), (1, 2, 6), (1, 2, 7), (1, 2, 8), (1, 3, 1), (1, 3, 2),
 (1, 3, 3), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 3, 7), (1, 3, 8),
 (1, 4, 1), (1, 4, 2) ... (4, 6, 7), (4, 6, 8)]

Edit:

To list only combinations whose sum of elements equals 13, you can iterate the result by testing item by item:

somam_treze = []
resultado = product(range(1, 5), range(1, 7), range(1, 9))
for x in resultado:
    if (x[0] + x[1] + x[2] == 13):
        somam_treze.append(x)

Or:

somam_treze = [ x for x in resultado if x[0] + x[1] + x[2] == 13]

Upshot:

>>> somam_treze
[(1, 4, 8), (1, 5, 7), (1, 6, 6), (2, 3, 8), (2, 4, 7), (2, 5, 6), (2, 6, 5), (3, 2, 8), (3, 3, 7), (3, 4, 6), (3, 5, 5), (3, 6, 4), (4, 1, 8), (4, 2, 7), (4, 3, 6), (4, 4, 5), (4, 5, 4), (4, 6, 3)]

I got these codes borrowed from here and from here.

  • 1

    Just remembering that function product returns a Generator, then if it is not necessary to access the full list, it is best to keep it as Generator and iterate on the same.

  • Thank you @Blogueira, that’s what I was looking for, I spent hours looking yesterday and the day before yesterday and I found nowhere talking about it, not even using two simple 6-sided data as we know Is it possible, using this same sample space of 192 combinations, to make it only give the results for a certain number? For example: being any order [(a,b,c)], I want all possible orders that result in 13, for example, so a+b+c = 13 so we have as results [(4,6,3); (4,5,4), (1,6,6), (5,5,3), ..

  • @Luiscarlosmoura added information - see 'Edit' at the end of the reply!

  • I don’t know how to thank you @Blogueira Thank you very much

  • @Blogger, as I noticed, I am very beginner in programming, and I’m having a small problem compiling the script I use Python with Spyder interface and I’m using these lines: somam_treze = [] result = product(range(1, 5), range(1, 7), range(1, 9)) for x in result: if (x[0] + x[1] + x[2] == 13): print(somam_treze.append(x))

  • Use the second example: somam_thirteen = [ x for x in resultado if x[0] + x[1] + x[2] == 13] - then in another line, write print(somam_treze).

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.