Generating a randomised list of values that do not repeat consecutively

Hi all!

I need help with the following.

I am trying to generate a partitioned list of indexes, from 0 to 3, randomised and such that they don’t repeat consecutively between branches

Example

0,1,3,2
1,2,0,3
2,1,3,0
2,0,3,1

and so forth.

Thank you in advance!

Gio

Hi @DraftVader,

Does this help?

KR,

RandomListOfNumbers.gh (14.8 KB)

Thank you Pedro, unfortunately not quite…

see how the last item of branch 0 is the same as the first item of branch 1

So maybe a better way of explaining, I wish to jitter a repeating list of 4 indexes, in such way that the last item of each branch is different from the first item of the next branch.

Cheers and thank you

A workaround I found is to jitter branches containing 1 to 3 and merging 0 after the fact, but doesnt quite achieve what i’m after

Maybe this is easier with a bit of code :slight_smile:

RandomListOfNumbers.gh (16.1 KB)

I test it with my permutation in Nautilus component and strangely it worked. Difference is never 0 also with bigger data.



Permutations

Element au the documenation of Nautilus
Inspiration

What I use

permutations.gh (6.6 KB)

1 Like

hi @DraftVader

import random

def shuffle_without_consecutive_repeats(lst):
    """Shuffles the input list ensuring no consecutive repetitions"""
    while True:
        random.shuffle(lst)
        if all(lst[i] != lst[i + 1] for i in range(len(lst) - 1)):
            return lst

def generate_partitioned_lists(num_partitions=4, num_indexes=4):
    """Generates a partitioned list of randomized indexes without consecutive repetition"""
    partitions = []
    last_partition = []

    for _ in range(num_partitions):
        new_partition = shuffle_without_consecutive_repeats(list(range(num_indexes)))

        # Ensure no consecutive repeat between partitions
        while last_partition and new_partition[0] == last_partition[-1]:
            new_partition = shuffle_without_consecutive_repeats(list(range(num_indexes)))

        partitions.append(new_partition)
        last_partition = new_partition

    return partitions

random.seed(42) 
partitions = generate_partitioned_lists()
for partition in partitions:
    print(partition)

You can integrate it in your GH
Hope this helps,
Farouk

Nothing random about permutations unless you make it so. CrossRef component does that or you can use this little bit of Python:

__author__ = "Joseph Oster"
__version__ = "2021.06.06"

from itertools import permutations  

perm = permutations(vals, len(vals))
perms = []
for c in list(perm): 
    perms.append("+".join(c))

P = perms


permutations_2024Jul15a.gh (10.0 KB)

To randomize, you can jitter the list of permutations and perhaps use a SubSet (Sub List) of them.


permutations_2024Jul15b.gh (11.3 KB)

1 Like

Thank you Joseph!

Thank you Farouk!

Thank you Laurent!

Thank you Pedro!

I just want to say how much I appreciate this community. You guys are always up for a challenge and consistently blow me away with your elegance of thought. Every time I ask a question I learn so much more than I expected.

A big thank you