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…
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…
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.
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: