0

I want to list all possible permutations of 0 and 1 six times for probability reasons.

I started out like this, thinking, it was a piece of cake; until all combinations are listed the code should keep on randomising the values. However once 0 and 1 are in the list it stops, i thought "for range in 6" would give something like this:

"0,0,0,0,0,0" (which is a possibility of 0 and 1)

"1,1,1,1,1,1" (which also is a possibility of 0 and 1)

My current output is "0, 1"

How can I group the items together of pieces of 6 and make the program keep filling values up unless a combination is already in my list?
I know there are 64 combinations altogether because of 2^6.

import math
import random

    a = 63;
    b = 1;
    sequences = [];
    while (b < a):
        for i in range(6):
            num = random.randint(0 , 1)
            if(num not in sequences):
               sequences.append(num)
               b += 1;
            else:
                b += 1;

    print(sorted(sequences));
coder
  • 12,832
  • 5
  • 39
  • 53
hvnwjxlg
  • 53
  • 1
  • 4

2 Answers2

2

I didn't understand if you are looking for a solution, or for a fix for your code. If you just look for a solution, you can do this much easier and more efficiently using itertools like so:

>>> from itertools import *
>>> a = list(product([0,1],repeat=6)) #the list with all the 64 combinations
coder
  • 12,832
  • 5
  • 39
  • 53
0

I understand you want to check in how many iterations you'll reach the 64 possible combinations. In that case this code does it:

import math
import random

combs = set()
nb_iterations = 0
while len(combs)<64:
    nb_iterations+=1
    a = (tuple(random.randint(0,1) for _ in range(6)))
    if a in combs:
        pass
    else:
        combs.add(a)

print("Done in {} iterations".format(nb_iterations))

I ran it a couple of times and it took around 300/400 iterations to get the full list.

Example of outputs:

Done in 313 iterations
Done in 444 iterations
Done in 393 iterations
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219