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

3

I'm doing a 3-sided sample space probability work (one of 4 sides, one of 6 sides and another of 8 sides)
I know that the sample space, without repetition between the data, is 4 * 6 * 8 = 192.
I saw in a topic 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 there were only two things missing:

  • possible combinations not to be repeated
  • that the data is from 1 to the respective faces.
  • As for example:

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

    Could someone help me?

    Edit (resolved) I did 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 faces as you want 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")   
    
        
    asked by anonymous 15.06.2017 / 03:06

    1 answer

    4

    If you are looking for a combination of the results of all 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 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 as follows:

    [(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 through 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]
    

    Result:

    >>> 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've borrowed those from here and data .

        
    16.06.2017 / 00:54