Generate distinct list of numbers using Random Reduce & Jitter

Dear Rhino Community,

I would like to create several distinct sets of indices (to select polygons). By changing the seed of “Random Reduce” I want to obtain several sets where:

  1. there are no repetitive indices within each set.
    For instance, this set (1,4,1) is not correct because there are two times “1”


  1. Each scenario is unique (hence the position of the indices in the list does not matter) For instance the sets (1,5,2) and (2,1,5) should be considered as the same set and should appear only once in the final list

I have used Jitter and Random Reduce. The first condition (no repetitive indices) has been already included in the model. I don’t know if it’s possible to manage the 2nd condition (distinct scenario).

Random (15.8 KB)

Thank you for the help!

Well… in my mind I imagined it simpler than this…
2019-03-13%2000_01_51-Window (24.7 KB)

It lacks descriptions inside…
The steps are something like this:

  • creating main set with series
  • creating sub-sets with random reduce (in a quantity more than requested)
  • internally sorting each sub-set
  • transforming every index in text string with zeroes (ex 6 > “006” or 75 > “075”)
  • merging every index of same set to get a “unique” string of concatenated indexes
  • creating a set those strings and removing the excess
  • deconstructing strings again into lists (branches) of indexes

Hope it helps

If I understand correctly you’re looking for a random bunch of combinations: sets of items with no repetition. The python itertools module is great for combinatorics stuff: counting, permutations, combinations… (9.4 KB)

You may find the attached “fun” as well. Works either in unique permutations (of length: take) or just samples the unique integers acc the take value in a tree. (122.0 KB)

WARNING: For a given collection of n items and permutation length pL all possible permutations are: factorial(n)/factorial(n-pL). This means BIG business. Thus if you choose permutations (as result) that number is calculated BEFORE any action and if you click yes … well … blame you, not me:

Thank you for the idea! I don’t understand how you check if there are duplicates (the last part of the Gh definition, why “x+y+u+z” ?, is there a way to connect this check up directly to the list output?

It looks like an easier way to create the lists, thank you! But these are not unique (the second condition I wrote).

thank you for the test! I will have a look at the code, I am not very familiar to this:)

… it already is.
The last [Int] component of the first big group is what you need… I made the checking part only during testing and then left it there, you can remove it.
For every set I made:
x - mass addition of the indexes in the subset; a subset of different indexes would likely have different result, but not always…
y - mass addition of the partial result of the previous step; again two different subsets should/might have different results
u and z are the same but starting with mass multiplication…
x+y+u+z to have a (probably) unique value for every different subset…
Probably best would have be doing xyu*z instead, but probably some integer decimal/rounding limit might be involved… i’m not sure.

I think you might be wrong about the python script. It generates combinations, combinations are always unique by definition:
Unless I’m misunderstanding - did you want the digits to be shuffled so that (2,1,3) is a possibility, for example?

Sorry for the misunderstanding, I may have expressed myself incorrectly.
What I meant is that:
if the set (1,2,3) appears then I DON’T want to get (2,1,3) or (3,1,2) or (2,3,1)…No matter the position.
So if the numbers 1, 2 and 3 appeared together, the next possible set which could be generated is maybe (1,2,4) or (4,2,3)

ok! I will check this out now:)

Yeah, if we are understanding one-another correctly then the script I provided does exactly what you want.

In the example screenshot I say that I want to pick numbers from 1 to 5, and I want sets of 3 numbers.
The output from the python component is the following list:

that is all the possible combinations of 3 numbers where the numbers are between one and five. Combinations are selections of items such that the order does not matter.
After the python script generates all these combinations, all you have to do is randomly pick some. In the screenshot example I picked five combinations using random reduce.

If you don’t believe it, try checking it yourself. Think of a triplet of numbers and see if it’s in the list above. Unless I am missing something here, this is what you’re after.

Riccardo’s idea of checking for uniqueness by doing a sum is probably fine for this case, but if you want a general way of picking combinations regardless of data type then the python script does it.

I did solve this problem in that way because i didn’t want sequential combinations (or sub-sets)… to have random polygons (back to first post);
Still, indeed, my method is not greatly optimized.

Ah, okay I think I get it now. So the OP doesn’t strictly want combinations. Unless I’ve confused myself even more. Sorry about that.

I was wondering about the numbers of scenarios possible. I have thought that the binomial coefficient was the formula I should have considered (so : factorial(n)/(factorial (pL) * factorial(n-pL)) )

But @PeterFotiadis wrote
For a given collection of n items and permutation length pL all possible permutations are:

Which one is the right formula while excluding repetitions such as (1,2,4) and (2,1,4)?

Get the trad update, the unique perm (new option: if result ==3) , the perm count calculator, the proof (see inside C# how it works) and be a happy bunny (I do hope). (120.9 KB)

Thank you very much for all these great solutions !!! It works very well into my Grasshopper model