players) and change this part of the list comprehension to players, where n = len(players). If you want to use something other than the numbers 1 to n, you can take it as a list (e.g. This uses the numbers 1 to n because of the formula 1 + (i + j) % n in the first list comprehension. While len(rows) > generate_permutations(5, 12) For additional randomness, it's safe to shuffle those r rows, but only after taking the slice. Generate enough Latin squares, put them on top of each other, and then slice r rows from it. Each column can't have equal frequencies for each number unless r % n = 0, but it's simple enough to guarantee that the frequencies in each column will differ by at most 1. Now let's solve the case where r > n, i.e. The upside is that this is guaranteed to work, and consistently in O(n^2) time, because it doesn't use rejection sampling or backtracking. I'm assuming that's OK since generating a uniform probability distribution over all possible outputs isn't one of the question requirements. Note that this algorithm can't generate all possible Latin squares by construction, the rows are cyclic permutations of each other, so you won't get Latin squares in other equivalence classes. Here's an implementation in Python: from random import shuffle Shuffling rows is a lot easier than shuffling columns, so we'll shuffle the rows, then take the transpose, then shuffle the rows again. The following always works for constructing a Latin square to start with: 1 2 3. These are easy to generate: start by constructing a Latin square, shuffle the rows, shuffle the columns, and then keep just the first r rows. In that case, your output must be a Latin rectangle or a Latin square. Let's starting by solving the case of generating n or fewer rows first. So in short, I want to give my function a number of permutations to generate, and the function should give me that number of permutations, in which no element appears on the same position more than the others (once, if all appear there once, twice, if all appear there twice, etc). User_idx_dict += īut I don't know how to finish the second function. # And this is my function that takes the stratified samples for permutations.ĭef generate_stratified_perms(player_list, number_of_samples): My code so far is : # this simply generates a number of permutations specified by number_of_samplesĭef generate_perms(player_list, number_of_samples): I have fixed number of 1s, and I want to permutate these fixed number of 1s in these N positions. I have N positions, and each position can be either 0 or 1. However, one could not add, for example, to the above, as there are two permutations in which 1 is on the first position twice, also 5 would be on the 5th position twice, however other elements are only present on those positions once. Permutation without duplicates in Python. I want to generate permutations of elements in a list, but only keep a set where each element is on each position only once.įor example could be a user list and I want 3 permutations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |