1

I have code for calculating the number of combinations or permutations for any given set.

(math.comb(n,k)) and (math.perm(n,k)) so perm = (math.perm(10,5)), perm == 30240 permutations

I would like to generate random permutations and combinations of n and k,
so if n = 10, and k = 5, I would get something like 3,7,1,2,9 as either a perm or comb

What maths function would I use for this ?

Edit:
The code should generate an array of k values from a selection of 10 randomly and for the combinations remove each value (k) as it is selected from the main set (n) so there are no repititions of (n). The branch of maths is combinatorics (if that helps)
I am certain there must be a function for this already created.
I have millions of arrays to generate and work through.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Temp_file
  • 41
  • 6
  • 1
    Does this answer your question? [Shuffle an array with python, randomize array item order with python](https://stackoverflow.com/questions/473973/shuffle-an-array-with-python-randomize-array-item-order-with-python) – Peter O. May 01 '20 at 14:17
  • 2
    30240 permutations is not too much, so you can just create a list of them, and then `random.shuffle` them. This way you'll get a random order of all possible permutations. Or if you need just a few of them, then `random.sample`. – eumiro May 01 '20 at 14:33
  • 1
    Look up ranking and unranking, if you are not already familiar with it. It's a method for converting random indices into the corresponding selection of n-choose-k. I'm not familiar with a numpy or scipy implementation. – Mad Physicist May 08 '20 at 18:51
  • 1
    What have you tried so far? Are you always looking for permutations in the order 30000? Do you have any sets in the order of 2000 elements? – MisterMiyagi May 11 '20 at 15:38
  • eumiro thankyou. The random shuffle, I think but cant work out why, gives a lightly different random than the random I am looking for. I need to do some work on this to work it out. – Temp_file Aug 05 '20 at 19:55
  • MisterMiyagi, I am looking for lists of permutations in the millions – Temp_file Aug 05 '20 at 19:56
  • 2
    Note that the default Python PRNG is a Mersenne Twister with a period of 2**19937-1. This is not sufficient to randomly draw/shuffle from the permutations of a set larger than 2080 elements. – MisterMiyagi Aug 05 '20 at 20:03
  • Where do people like you learn this stuff ? Ive done three courses and they are all like: this is a variable, this is a list..... very difficult to learn python I think – Temp_file Aug 05 '20 at 20:11
  • 1
    That is general programming knowledge, not language specific. You will have to look at the actual topics (e.g. (pseudo) random number generators) instead of just Python. Once you know what to look for, you will understands [the notes in the documentation](https://docs.python.org/3/library/random.html#random.shuffle). – MisterMiyagi Aug 05 '20 at 20:21

1 Answers1

2

You can use the random module of Python which is preincluded same as math (so you don't need to install it using pip etc.). First define the set that you want to choose k elements randomly from them, in your example it is the set of integers from 1 to 10, so I use S=[i for i in range(1,11)] (remember the last element in range is the one before the integer you give it as the termination of the range). Then for receiving k elements with replacement use choices command in random package and for the case without replacement use sample command. The codes are in below.

import random
S=[i for i in range(1,11)]
A=random.choices(S,k=5)
B=random.sample(S,5)

For example when I ran the above code after asking python to show me S, A and B respectively, I received the following result.

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[8, 3, 2, 6, 8]
[3, 9, 6, 1, 10]

You can also use numpy module to combine the two steps of defining S with a range and then using numpy.random.choice. But note that, you will have k choices from a the set of 10 integers started from 0 not 1. To choose from a set not defined as n sequel integers starting from 0, you should define it again same as our first method and give it to this command instead of only a number such as 10. To make the choices to be done without replacement, you should add an option replace=False. See the following code.

import numpy
C=numpy.random.choice(10,5)
D=numpy.random.choice(10,5,replace=False)
S=[i for i in range(1,11)]
A=numpy.random.choice(S,5)
B=numpy.random.choice(S,5,replace=False)

After running this and asking the sets C, D, A and B respectively, I got the followings.

array([5, 5, 1, 4, 7])
array([0, 7, 4, 6, 1])
array([10,  5,  7,  1,  7])
array([6, 5, 7, 8, 1])