BubbleMonster - 1 year ago 130

Python 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]`

`[44,45,46,47,48,49]`

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

`[]`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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
```

*).*

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**