Python – all lottery combinations – no duplicates, in order
Question:
I’ve wrote a script to print out lottery combinations. What i’m aiming for is this: There are 6 numbers in the lottery between 1-49 meaning there are 13,983,816 combinations. I want to print out all combinations IN ORDER whilst making sure there are NO duplicates.
Here is my code so far:
import random
numbers = []
for i in range(2):
for j in range(6):
numbers.append(random.randint(1,49))
for k in range(j):
while numbers[j]==numbers[k]:
numbers[j]=random.randint(1,49)
print sorted(numbers)
numbers = []
f = open('combinations.txt', 'w')
f.write(str(sorted(numbers)))
The problems are:
The output in the terminal is:
[18, 20, 27, 32, 44, 48]
[5, 7, 10, 13, 33, 45]
I want to start with [1,2,3,4,5,6]
and end up at [44,45,46,47,48,49]
. So I need to order the results.
Also, I’ve tried converting the list to a string so I can put the results in a big text file, but I’m just printing []
to the text file at the moment.
Answers:
>>> from itertools import combinations
>>> for comb in combinations(range(1,50), 6):
... print comb #Hit Enter at your own risk
For printing the combinations to the text file:
with open('combinations.txt', 'w') as f:
for comb in combination:
f.write(str(comb) + 'n')
You were clearing your list then writing to the file.
from itertools import combinations
f = open('combinations.txt', 'w')
for comb in combinations(range(1,50), 6):
f.write(str(comb))
f.write('n')
f.close()
But make sure you have at least 350 Megabytes of disk space free! and some time to spare.
(348168480 bytes I checked with:
>>> s = 0
>>> for comb in combinations(range(1,50), 6):
... s += len(repr(comb))+2
...
>>> s
348168480
).
**import itertools
f= open('combinations.txt','w')
numb = [1,2,3,4,5,6,7]
it = itertools.combinations(numb,3)
for x in it:
f.write(str(x))
f.write('n')
f.close()**
There you go, just add as many numbers to numb and change the it variable to correspondent of r
import time
from datetime import timedelta
start_time = time.time()
from itertools import combinations
comb_list = list(combinations(range(1, 50), 6))
start_index = comb_list.index((1, 2, 3, 4, 5, 6))
counter = 0
for i, comb in enumerate(comb_list[start_index:]):
print(f"Combination {i+1}: {comb}")
counter += 1
end_time = time.time()
elapsed_time = end_time - start_time
elapsed_time = timedelta(seconds=elapsed_time)
print("Elapsed time:", elapsed_time)
#Combination 1: (1, 2, 3, 4, 5, 6)
#Combination 13983816: (44, 45, 46, 47, 48, 49)
#Elapsed time: 0:03:33.373467
I’ve wrote a script to print out lottery combinations. What i’m aiming for is this: There are 6 numbers in the lottery between 1-49 meaning there are 13,983,816 combinations. I want to print out all combinations IN ORDER whilst making sure there are NO duplicates.
Here is my code so far:
import random
numbers = []
for i in range(2):
for j in range(6):
numbers.append(random.randint(1,49))
for k in range(j):
while numbers[j]==numbers[k]:
numbers[j]=random.randint(1,49)
print sorted(numbers)
numbers = []
f = open('combinations.txt', 'w')
f.write(str(sorted(numbers)))
The problems are:
The output in the terminal is:
[18, 20, 27, 32, 44, 48]
[5, 7, 10, 13, 33, 45]
I want to start with [1,2,3,4,5,6]
and end up at [44,45,46,47,48,49]
. So I need to order the results.
Also, I’ve tried converting the list to a string so I can put the results in a big text file, but I’m just printing []
to the text file at the moment.
>>> from itertools import combinations
>>> for comb in combinations(range(1,50), 6):
... print comb #Hit Enter at your own risk
For printing the combinations to the text file:
with open('combinations.txt', 'w') as f:
for comb in combination:
f.write(str(comb) + 'n')
You were clearing your list then writing to the file.
from itertools import combinations
f = open('combinations.txt', 'w')
for comb in combinations(range(1,50), 6):
f.write(str(comb))
f.write('n')
f.close()
But make sure you have at least 350 Megabytes of disk space free! and some time to spare.
(348168480 bytes I checked with:
>>> s = 0
>>> for comb in combinations(range(1,50), 6):
... s += len(repr(comb))+2
...
>>> s
348168480
).
**import itertools
f= open('combinations.txt','w')
numb = [1,2,3,4,5,6,7]
it = itertools.combinations(numb,3)
for x in it:
f.write(str(x))
f.write('n')
f.close()**
There you go, just add as many numbers to numb and change the it variable to correspondent of r
import time
from datetime import timedelta
start_time = time.time()
from itertools import combinations
comb_list = list(combinations(range(1, 50), 6))
start_index = comb_list.index((1, 2, 3, 4, 5, 6))
counter = 0
for i, comb in enumerate(comb_list[start_index:]):
print(f"Combination {i+1}: {comb}")
counter += 1
end_time = time.time()
elapsed_time = end_time - start_time
elapsed_time = timedelta(seconds=elapsed_time)
print("Elapsed time:", elapsed_time)
#Combination 1: (1, 2, 3, 4, 5, 6)
#Combination 13983816: (44, 45, 46, 47, 48, 49)
#Elapsed time: 0:03:33.373467