# How can I generate a random Boolean pattern to control a trance gate in TidalCycles?

**URL:** <https://uzu.lurk.org/t/how-can-i-generate-a-random-boolean-pattern-to-control-a-trance-gate-in-tidalcycles/5981>\
**Category:** TidalCycles\
**Tags:** questions\
**Created:** [23 October 2025 09:20 UTC](https://uzu.lurk.org/t/how-can-i-generate-a-random-boolean-pattern-to-control-a-trance-gate-in-tidalcycles/5981 "2025-10-23T09:20:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![a00](https://uzu.lurk.org/user_avatar/uzu.lurk.org/a00/32/3341_2.png) [@a00](https://uzu.lurk.org/u/a00)\
**Post date:** [23 October 2025 09:20 UTC](https://uzu.lurk.org/t/how-can-i-generate-a-random-boolean-pattern-to-control-a-trance-gate-in-tidalcycles/5981/1 "2025-10-23T09:20:57Z")

</div>

Continuing the discussion from [How to implement a tranceGate control pattern in TidalCycles?](https://club.tidalcycles.org/t/how-to-implement-a-trancegate-control-pattern-in-tidalcycles/5966):

Here is a more concise version of `trancegate`, based directly on the original implementation by @Yaxu:

```haskell
let trancegate i r = trigZeroJoin . fmap (\b -> ampbus i $ if b then slow r (when (<1) (const $ segment 8 isaw) $ pure 1) else 0.7)
in do
  d1 $ note "-14" # sound "supersaw" # trancegate 1 0.05 ("1011010110011101") # gain 0.8
  d2 $ s "kick:6*4"

```

Is it possible to create a helper function, for example `randBits`, that takes a number `n` and outputs a Boolean pattern of length `n` which can be directly used as the third argument of `trancegate`? The idea is to randomly determine which slices the gate closes on, so that I can write something like:

```haskell
let trancegate i r = trigZeroJoin . fmap (\b -> ampbus i $ if b then slow r (when (<1) (const $ segment 8 isaw) $ pure 1) else 0.7)
in do
  d1 $ note "-14" # sound "supersaw" # trancegate 1 0.05 (randBits 16) # gain 0.8
  d2 $ s "kick:6*4"

```

Any suggestions or examples for implementing `randBits` in TidalCycles would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![yaxu](https://uzu.lurk.org/user_avatar/uzu.lurk.org/yaxu/32/2_2.png) [@yaxu](https://uzu.lurk.org/u/yaxu)\
**Post date:** [23 October 2025 09:42 UTC](https://uzu.lurk.org/t/how-can-i-generate-a-random-boolean-pattern-to-control-a-trance-gate-in-tidalcycles/5981/2 "2025-10-23T09:42:05Z")

</div>

```haskell
randBits n = segment n ((/= 0) <$> irand 2)

```

How about the above?

---

<div class="post-metadata">

**Author:** ![a00](https://uzu.lurk.org/user_avatar/uzu.lurk.org/a00/32/3341_2.png) [@a00](https://uzu.lurk.org/u/a00)\
**Post date:** [23 October 2025 20:44 UTC](https://uzu.lurk.org/t/how-can-i-generate-a-random-boolean-pattern-to-control-a-trance-gate-in-tidalcycles/5981/3 "2025-10-23T20:44:14Z")

</div>

Thanks so much for this, @yaxu! That’s incredibly concise and elegant. I love how randBits gives a perpetually unpredictable gating pattern. Perfect for what I wanted. Really appreciate the quick solution:

```haskell
let trancegate i r = trigZeroJoin . fmap (\b -> ampbus i $ if b then slow r (when (<1) (const $ segment 8 isaw) $ pure 1) else 0.7)
    randBits n = segment n ((/= 0) <$> irand 2)
in do
  d1 $ note "-14" # sound "supersaw" # trancegate 1 0.05 (randBits 16) # gain 0.8
  d2 $ s "kick:6*4"

```

I found that I sometimes need a random pattern to be generated once and have it repeat every cycle. I made a binary convertor such that:

```haskell
let trancegate i r = trigZeroJoin . fmap (\b -> ampbus i $ if b then slow r (when (<1) (const $ segment 8 isaw) $ pure 1) else 0.7)
in do
  d1 $ note "-14" # sound "supersaw" # trancegate 1 0.05 ("1011010110011101") # gain 0.8
  d2 $ s "kick:6*4"

```

is the same as:

```haskell
let trancegate i r = trigZeroJoin . fmap (\b -> ampbus i $ if b then slow r (when (<1) (const $ segment 8 isaw) $ pure 1) else 0.7)
    bin n=fastcat$map pure$reverse$let f x|x==0=[]|1>0=(x`mod`2==1):f(x`div`2)in if n==0 then[False]else f n
in do
  d1 $ note "-14" # sound "supersaw" # trancegate 1 0.05 (bin 46493) # gain 0.8
  d2 $ s "kick:6*4"

```

Of course, for 8-bit numbers we can use`binary:`

```haskell
let trancegate i r = trigZeroJoin . fmap (\b -> ampbus i $ if b then slow r (when (<1) (const $ segment 8 isaw) $ pure 1) else 0.7)
in do
  d1 $ note "-14" # sound "supersaw" # trancegate 1 0.05 (binary $ pure 241) # gain 0.8
  d2 $ s "kick:6*4"

```

After playing with the code all day and trying bit scrambling, I ended up using seed-based hashing to generate fixed-length deterministic pseudo-random bits:

```haskell
let trancegate i r = trigZeroJoin . fmap (\b -> ampbus i $ if b then slow r (when (<1) (const $ segment 8 isaw) $ pure 1) else 0.7)
    rngBin s l = fastcat $ map pure $ take l $ f ((1103515245*s+12345) `mod` 2^64)
      where f 0 = repeat False; f x = (x `mod` 2==1):f(x`div`2)
in do
  d1 $ note "-14" # sound "supersaw" # trancegate 1 0.06 (rngBin 14 16) # gain 0.8
  d2 $ s "kick:6*4"

```

I’m just starting out with TidalCycles, and this makes experimenting with patterns so much more approachable. Thank you!

---

<div class="post-metadata">

**Author:** ![cleary](https://uzu.lurk.org/user_avatar/uzu.lurk.org/cleary/32/177_2.png) [@cleary](https://uzu.lurk.org/u/cleary)\
**Post date:** [4 November 2025 06:21 UTC](https://uzu.lurk.org/t/how-can-i-generate-a-random-boolean-pattern-to-control-a-trance-gate-in-tidalcycles/5981/4 "2025-11-04T06:21:42Z")

</div>

> [@a00](#):
>
> I found that I sometimes need a random pattern to be generated once and have it repeat every cycle. I made a binary convertor such that:

Have a look at the `timeLoop` function for this as well - @polymorphic.engine put me onto it in this blog post:

> **[Tidal Profile - polymorphic\_engine | Tidal Cycles](https://tidalcycles.org/blog/tidal_profile_polymorphic_engine/)**
>
> | Tidal Cyclist | Martin Gius |

---

<div class="post-metadata">

**Author:** ![kit-christopher](https://uzu.lurk.org/user_avatar/uzu.lurk.org/kit-christopher/32/686_2.png) [@kit-christopher](https://uzu.lurk.org/u/kit-christopher)\
**Post date:** [6 November 2025 03:10 UTC](https://uzu.lurk.org/t/how-can-i-generate-a-random-boolean-pattern-to-control-a-trance-gate-in-tidalcycles/5981/5 "2025-11-06T03:10:04Z")

</div>

hey @yaxu, I was curious what I would need to do to alter this so that it could be passed into the second argument of `binaryN`: ex `d1 $ struct (binaryN 16 (randBits 16)) $ "s “bd”`. it worked fine for the `trancegate` example but not for the `binaryN` experiment.

---

<div class="post-metadata">

**Author:** ![yaxu](https://uzu.lurk.org/user_avatar/uzu.lurk.org/yaxu/32/2_2.png) [@yaxu](https://uzu.lurk.org/u/yaxu)\
**Post date:** [12 November 2025 16:39 UTC](https://uzu.lurk.org/t/how-can-i-generate-a-random-boolean-pattern-to-control-a-trance-gate-in-tidalcycles/5981/6 "2025-11-12T16:39:29Z")

</div>

`binaryN` accepts a pattern of integers, so you can `binaryN 8 (segment 1 $ irand 8)`.

`randBits` produces a pattern of boolean values, so it doesn’t really make sense to pass that to `binaryN`.

---

<div class="post-metadata">

**Author:** ![kit-christopher](https://uzu.lurk.org/user_avatar/uzu.lurk.org/kit-christopher/32/686_2.png) [@kit-christopher](https://uzu.lurk.org/u/kit-christopher)\
**Post date:** [12 November 2025 18:12 UTC](https://uzu.lurk.org/t/how-can-i-generate-a-random-boolean-pattern-to-control-a-trance-gate-in-tidalcycles/5981/7 "2025-11-12T18:12:23Z")

</div>

sweet! so simple 🙂 thanks @yaxu
