# Pattern of 0s and 1s without repeating more than two times

Hi guys,

How to create a pattern of 0s and 1s without any number repeating more than two times consecutively in grasshopper?

For example, if a use random and convert to 0 and 1, I will have something like this (0,1,0,1,1,1,1,0,0,0,1,1,0…)

What i need is something (0,1,0,1,1,0,0,1,0,0,1,0,1,0…) Notice that they repeat max 2 times… how to lock that max? And maybe i can change to 3 times consecutively or more…

Thanks

You could use an L-system where you repeatedly replace each [0] with [1,0], and each [1] with [0,1].
So the first few generations would be
0
10
0110
10010110
0110100110010110
and so on…

1 Like

Actually I have a list length and need the list to follow the same length… not sure how to achieve that with L system…

If you don’t find a solution you could generate a bigger list using L System and split it.

probably do this through a logical formula. what you want is a random 0 or 1, when the last 2 numbers are not the same. So if the sum of last two numbers = 0, or = 2, then this number cannot be the same as the last number, else it is a random 0 or 1. which you can do with an If Then Else formula

Random roll the chunk sizes of consecutive 0s or 1s, instead of the number themselves.
You can increase the first slider to have a bigger maximum repetition.

random.gh (9.1 KB)
(As random numbers are rounded to ceiling , the Maximum 1 part is to avoid the unlikely roll of a pure 0…)

Edit: this method would get you repetitions of single 0s/1s with the same probability of the biggest maximum repetition. While actual rolls of 0s/1s would exponentially reduce the chance of big repetitions…
It might not behave as you intended with big maximum repetitions.

2 Likes

You can also take a random sequence of 0s and 1s and apply a single round of the substitution I mentioned above to guarantee there are never more than 2 of the same in a row. This also makes it easy to generate a sequence of given length:

binaryseq.gh (7.5 KB)

(this makes me think a little of Conway’s musical sequences)
http://www.neverendingbooks.org/conways-musical-sequences/

http://www.neverendingbooks.org/conways-musical-sequences-2/

6 Likes

Thank you very much! Works nice!

Thank you so much! Simple and it works!!