This little code shows 6 random integer numbers from 1 to 50 ...

``````import random

print "show 6 random integers from 1 to 50:"
for k in range(6):
r = random.randint(1, 50)
print r,
print``````

Rewrite the code to make sure there are no duplicate numbers amongst the six numbers displayed.

Now let the user enter six unique numbers from 1 to 50 and then count how many tries it takes the computer to match 3, 4, 5 or 6 of the user's numbers, using the above random number example. You might recognize this as a way to figure out the chances to win a lotto game.

Not sure if this is the place to ask but I want to write a similar program that list all possible combinations and have it displayed in the Notepad program. Where do I start? And how do I write a search program (with parameters that I set) that filters the results?

Thanks!

Editor's Note:
This question has been moved to the forum from Projects for Beginners.
Sorry, the title should really be combinations

Thanks for the snippet but I think the results are permutations. Combination is when the order of the sequence doesn't matter. Where 1,2,3=3,2,1=1,3,2=2,1,3 etc.

How do I fix that snippet?

If sample_size < len(iterable) you have a combination
If sample_size == len(iterable) you have a permutation

Here is an example ...

``````def combinations(itr, n):
"""
combinations(items, n) is a recursive generator function where
itr is an iterable object like a list or string
n is the sample size
returns a list of nonrepeating sample size items
n = len(itr) is the max sample size and gives a permutation
"""
if n == 0:
yield []
else:
for k in range(len(itr)):
# a recursive function
for c in combinations(itr[:k] + itr[k+1:], n - 1):
yield [itr[k]] + c

iterable = [1, 2, 3]
# this will be a combination sample_size < len(iterable)
sample_size = 2
combi_list = list(combinations(iterable, sample_size))
print( combi_list)

print( '-'*50 )

# this will be a permuatation
sample_size = len(iterable)
combi_list2 = list(combinations(iterable, sample_size))
print( combi_list2)

"""my result -->
[[1, 2], [1, 3], [2, 1],
[2, 3], [3, 1], [3, 2]]
--------------------------------------------------
[[1, 2, 3], [1, 3, 2], [2, 1, 3],
[2, 3, 1], [3, 1, 2], [3, 2, 1]]
"""``````

I think badboy00z is correct. You can use module itertools to show it:

``````# combinations and permutations using Python3 itertools

import itertools

iterable = [1, 2, 3]
# sample_size < len(iterable)
sample_size = 2
combi_list = list(itertools.combinations(iterable, sample_size))
print(combi_list)

print( '-'*50 )

sample_size = 2
combi_list2 = \
list(itertools.combinations_with_replacement(iterable, sample_size))
print(combi_list2)

print( '-'*50 )

perm_list = list(itertools.permutations(iterable))
print(perm_list)

print( '-'*50 )

perm_list2 = list(itertools.permutations(iterable, 2))
print(perm_list2)

""" my result -->
[(1, 2), (1, 3), (2, 3)]
--------------------------------------------------
[(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
--------------------------------------------------
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
--------------------------------------------------
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
"""``````

It looks like badboy00z is correct. The last result is really a permutation with a sample size of 2.

It looks like badboy00z is correct. The last result is really a permutation with a sample size of 2.

Thanks for the code example, I stand corrected.

The thread title is a bit misleading IMO. I actually want a list of all combinations and not random numbers. "Number combinations generator" is a better title.

Even though that code is suppose to be basic it still seems complicated for something so simple.

After searching for awhile I managed to find a program that does exactly what I need it to do. You guys can check it out here. The only downside is that you need to register and pay \$20 to use its full potential.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.