# Randomised Sequences that Stick

**URL:** <https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838>\
**Category:** Creations & Ideas\
**Created:** [December 23, 2020, 8:33am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838 "2020-12-23T08:33:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![soxsa](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/soxsa/32/1669_2.png) [@soxsa](https://in-thread.sonic-pi.net/u/soxsa)\
**Post date:** [December 23, 2020, 8:33am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/1 "2020-12-23T08:33:31Z")

</div>

Inspired by @martin’s code (thank you) here’s my go at a randomised sequence generator, where the sequence stays stuck until I want to change it. I’m sure this will be useful.

But this is the point where someone gently points out that Sam’s written an in-built one already.

I’m starting to want to have my own function library - is that possible?

```ruby
#Sequencer Generator
#Randomised sequences that stick
use_bpm 80

define :getseq do |length, notes, seed|
  #Generate a randomised sequence based on the seed
  seq=[]
  use_random_seed seed
  for i in 0..length-1
    seq.push(notes[rrand_i(0,notes.length-1)])
  end
  seq.ring
end

notes = scale :C4, :major, num_octaves: 1

#Change the sequence length and seed
seq1 = getseq(7,notes,1)

live_loop :main do
  8.times do
    play seq1.tick, amp: 0.2
    sleep 0.25
  end
end

live_loop :bass, sync: :main do
  play :C3, amp: 0.2
  sleep 1
end

```

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [December 23, 2020, 9:52am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/2 "2020-12-23T09:52:14Z")

</div>

You can define individual functions, and then allow them to be available on Sonic Pi start up using `init.rb` - is that close enough?

---

<div class="post-metadata">

**Author:** ![soxsa](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/soxsa/32/1669_2.png) [@soxsa](https://in-thread.sonic-pi.net/u/soxsa)\
**Post date:** [December 23, 2020, 9:53am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/3 "2020-12-23T09:53:23Z")

</div>

Yes that’s ideal, thanks

---

<div class="post-metadata">

**Author:** ![soxsa](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/soxsa/32/1669_2.png) [@soxsa](https://in-thread.sonic-pi.net/u/soxsa)\
**Post date:** [December 23, 2020, 9:54am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/4 "2020-12-23T09:54:59Z")

</div>

> [@soxsa](#):
>
> Sam’s written an in-built one already.

In fact I think that something like this is just a useful - slightly different effect but easier to code live! A randomised sequence that repeats - that’s the key thing. My ideal would be a sequences that gradually evolves rather than hops between states.

```ruby
use_random_seed 5
    seq = notes.shuffle
    in_thread do
      16.times do
        osc "/midi4x4",seq.tick,127,0.1,1,1
        sleep 0.25
      end
end

```

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [December 23, 2020, 9:55am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/5 "2020-12-23T09:55:50Z")

</div>

Re function library, I do a similar thing by defining my utility functions in a dedicated file, and then using `run_file` in init.rb 👍

---

<div class="post-metadata">

**Author:** ![soxsa](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/soxsa/32/1669_2.png) [@soxsa](https://in-thread.sonic-pi.net/u/soxsa)\
**Post date:** [December 23, 2020, 9:57am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/6 "2020-12-23T09:57:11Z")

</div>

That sounds good, I’ll experiment

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [December 23, 2020, 10:17am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/7 "2020-12-23T10:17:54Z")

</div>

NB if you use the latest 3.3beta on Mac released to Patreon supporters today then be aware that the location of init.rb has moved to `~/.sonic-pi/config/init.rb` from `~/.sonic-pi/init.rb`

---

<div class="post-metadata">

**Author:** ![soxsa](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/soxsa/32/1669_2.png) [@soxsa](https://in-thread.sonic-pi.net/u/soxsa)\
**Post date:** [December 23, 2020, 10:28am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/8 "2020-12-23T10:28:23Z")

</div>

Windows and Raspberry Pi me.

The ring operations take, shuffle etc. are rich pickings for this directed randomness. But I’ve decided what I need is a shuffle that where I can control the amount of shuffling if you see what I mean. I think I should be able to do that. Like you specify how many elements get swapped, or how far apart they are.

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [December 23, 2020, 10:52am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/9 "2020-12-23T10:52:25Z")

</div>

I built my own prototype version of exactly that too a little while ago. Haven’t used it for a little while, so I forget if it was fully tested and working 😂 but I was also wanting such a thing. I am hopeful that a version of it will make its way into the official functions.

---

<div class="post-metadata">

**Author:** ![soxsa](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/soxsa/32/1669_2.png) [@soxsa](https://in-thread.sonic-pi.net/u/soxsa)\
**Post date:** [December 23, 2020, 11:27am UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/10 "2020-12-23T11:27:17Z")

</div>

Ah ah, great minds 😄 I think this would be really good for the live stuff I’m working on now, so I’ll give it a go too.

EDIT mind you, the existing ring functions are at least as rich as what you get in a modular sequencer and more flexible in combo, so maybe I should exhaust those possibilities before chomping at the bit for new functions. Users…can’t live with them…

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [December 23, 2020, 4:25pm UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/11 "2020-12-23T16:25:35Z")

</div>

I have found the new `use_random_stream` command in latest SP3.3.0beta useful for controlling what is selected. I itilised it for example in my [beethoven mashup program](https://in-thread.sonic-pi.net/t/remixing-beethoven/4643/3) I haven’t published this yet because it contained this command, which I used in my own build. I changed the selection during the program using `use_random_stream [:white,:pink].choose` to change the effect of subsequent “random” commands.

---

<div class="post-metadata">

**Author:** ![soxsa](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/soxsa/32/1669_2.png) [@soxsa](https://in-thread.sonic-pi.net/u/soxsa)\
**Post date:** [December 23, 2020, 4:57pm UTC](https://in-thread.sonic-pi.net/t/randomised-sequences-that-stick/4838/12 "2020-12-23T16:57:41Z")

</div>

Oh yes, that does look interesting. With a bit of experimenting now I have found some nice ways of using the ring functions to give more of evolving feel to a sequence. I guess at bottom I’m trying to hear some of the conventional ways to cadance a solo - increasing complexity, embellishment, motivic development. Simply adding more randomness (which is the easiest to do) is a way, but a bit of a one-trick pony. I think this new `use_random_stream` could help things along.
