# More real randomization

**URL:** <https://in-thread.sonic-pi.net/t/more-real-randomization/608>\
**Category:** \[P\] Patreon Support\
**Created:** [January 11, 2018, 10:20am UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608 "2018-01-11T10:20:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![matas](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/matas/32/354_2.png) [@matas](https://in-thread.sonic-pi.net/u/matas)\
**Post date:** [January 11, 2018, 10:20am UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/1 "2018-01-11T10:20:35Z")

</div>

Hi, I have opened an issue ticket in Github, and only later discovered this forum. My ticket is rather a question, so admins, please feel free to just close it on github if you will.  
Here I paste what I wrote there:

I have read documentation and tutorials on random methods. I do understand that reproducible behavior is in the philosophy of Sonic pi.  
However I am trying to come up with a way to get random number on each run. It does not have to be random per se, I only want it appear random, i.e. different on every run. I would use it as an argument for use\_random\_seed.  
The reason I want this kind of behavior is because I’m using Sonic pi for my guitar practice. I’m trying to build routines that would print out random notes, chords, scales, etc from predefined collections and then play them after some sleep time. The idea is that this playback should be as a reference or confirmation for me that what I played on my guitar was indeed what I was supposed to play.  
Now I do understand that I can edit the seed before every run, but that is not the level of automation that I am aiming for.  
Here comes my question: is there a method in Sonic pi DSL, or accessible outside of DSL that I could use to make a seed from it’s return? A hash, a time-stamp, or any other value that I could process into a different value on every run?

Any ideas appreciated.  
TIA

---

<div class="post-metadata">

**Author:** ![Martin](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/martin/32/157_2.png) [@Martin](https://in-thread.sonic-pi.net/u/Martin)\
**Post date:** [January 11, 2018, 11:31am UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/2 "2018-01-11T11:31:04Z")

</div>

Hi,

maybe you could use something like:

```ruby
t = Time.now.to_i
use_random_seed t
puts rand

```

which uses a `timestamp` to change the random\_seed constantly. But - as @samaaron told on different occasions - using native ruby stuff is a bit like the Wild West. You should not be sure whether and how long the laws of Sonic Pi will accept and support that. But the code bit above seems to work. Someone with more internal knowledge might have a better idea.

---

<div class="post-metadata">

**Author:** ![Eli](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/eli/32/116_2.png) [@Eli](https://in-thread.sonic-pi.net/u/Eli)\
**Post date:** [January 11, 2018, 4:43pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/3 "2018-01-11T16:43:16Z")

</div>

Sounds like a simple use of rand\_i(number) would work here…

Eli…  
,  
live\_loop :loopy do

number = rand\_i(10000)  
puts number  
use\_random\_seed number  
sleep 5

end

---

<div class="post-metadata">

**Author:** ![matas](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/matas/32/354_2.png) [@matas](https://in-thread.sonic-pi.net/u/matas)\
**Post date:** [January 11, 2018, 8:23pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/4 "2018-01-11T20:23:12Z")

</div>

Martin, thank you this is just what I needed. I should have thought of it my self 🙂 I know it’s ruby and at the same time it’s not. Didn’t even try using Time class methods - thought it wasn’t accessible 😃

I love ruby, it is a shame it is stripped down in SonicPi, although I understand the motivation behind this.

---

<div class="post-metadata">

**Author:** ![matas](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/matas/32/354_2.png) [@matas](https://in-thread.sonic-pi.net/u/matas)\
**Post date:** [January 11, 2018, 8:33pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/5 "2018-01-11T20:33:19Z")

</div>

Eli, thank you, but this is not the solution. As you may or may not know all randomization functions in SonicPi are not actually random. They just appear random.  
`rand_i(10000)` will always return 7500 on first iteration of any run. Also every sequential iterations will follow the same pattern. This behavior is intentional in SonicPi as the code should always produce identical outcome on every run. In other words the behavior has to be reproducible.  
However for my specific use for guitar practice, I did’n want the pattern to be the same on every run. And for that I need an ever changing seed. As in Martin’s reply, Time.now() provides me exactly that.

---

<div class="post-metadata">

**Author:** ![Eli](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/eli/32/116_2.png) [@Eli](https://in-thread.sonic-pi.net/u/Eli)\
**Post date:** [January 11, 2018, 9:05pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/6 "2018-01-11T21:05:34Z")

</div>

> [@matas](#):
>
> It does not have to be random per se, I only want it appear random,

Hokay… I misread your intention. 🙂

Eli…

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [January 12, 2018, 3:08pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/7 "2018-01-12T15:08:25Z")

</div>

Hi, @Martin is right:

```ruby
use_random_seed Time.now.to_i

```

is the best approach for doing what you want. He’s also right in that it’s unsupported, so behaviour might change in the future, but don’t let that stop you from using it!

---

<div class="post-metadata">

**Author:** ![kniknoo](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/kniknoo/32/146_2.png) [@kniknoo](https://in-thread.sonic-pi.net/u/kniknoo)\
**Post date:** [January 16, 2018, 8:19pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/8 "2018-01-16T20:19:13Z")

</div>

Thanks y’all, been meaning to ask that one for a while, I had a feeling it would be something like that, how I learned to do true random in lua.

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [January 20, 2018, 2:34am UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/9 "2018-01-20T02:34:19Z")

</div>

#keys E(4)…G(7)…(A9)…C(0)…D(2)

key=9; mn=[];for jn in 1…1024  
mn[jn]=key+(hz\_to\_midi(11\*jn)); end  
mn[0]=:r

c=[9,8,6,9, 6,6,6,6, 8,8,6,6];o=[1,2,4]  
m=[2,0,4,0,3,4,0,2,4,0,4,0,3,4,0,0]

use\_bpm 80  
live\_loop :keys do  
for i in 0…95; x=i/8; y=i%16  
r=rand\_i(3); s=rand\_i(3); t=rand\_i(3)  
synth :piano, note: mn[c[x]\*m[y]\*o[r]]-24  
synth :piano, note: mn[c[x]\*m[y]\*o[s]]-24  
synth :piano, note: mn[c[x]\*m[y]\*o[t]]-24  
sleep 0.25; end; end

---

<div class="post-metadata">

**Author:** ![rubertres](https://avatars.discourse-cdn.com/v4/letter/r/e68b1a/32.png) [@rubertres](https://in-thread.sonic-pi.net/u/rubertres)\
**Post date:** [January 20, 2018, 10:52am UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/11 "2018-01-20T10:52:34Z")

</div>

Hello.

For more real random I use

```ruby
r=Random.rand(1..10) 

```

for random numbers from 1 to 10 included.

---

<div class="post-metadata">

**Author:** ![Martin](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/martin/32/157_2.png) [@Martin](https://in-thread.sonic-pi.net/u/Martin)\
**Post date:** [January 23, 2018, 5:08pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/12 "2018-01-23T17:08:51Z")

</div>

Hi,

I just have had some time on a train and went nuts with tick and randomness… I came up with that code:

```ruby
live_loop :ambient do
  t = Time.now.to_i
  use_random_seed t
  puts "Time: #{t}"
  z = [58,60,63,66,68,70,72,74].choose
  n = ((chord :c, :m7) + [z]).sort
  v = (line 0.25, 1, steps: 100).reflect
  p = (line -0.85, 0.85, steps: 100).reflect
  with_fx :reverb, room: 0.75, mix: rrand(0,1) do
    s = synth :fm, note: n.tick(:n), attack: [0,0,0,0,2].choose, release: [0.5,0.5,0.5,2].choose, amp: v.tick, pan: p.look, depth: 0.1, divisor: 1
    control s, amp: v.tick, amp_slide: 0.25, pan: p.look, pan_slide: 0.25, depth: 2, depth_slide: 1.5
  end
  sleep 0.25
end

```

Nothing really special, but what I like about it are two things: 1. seems to have a somehow purposeful harmonic sequence (for me despite the disharmonic bits in it) allthough it hasn’t actually. At least not from the constructional perspective. 2. There seem to be two independant voices at work but there is only one line (where the attack/release switches between long and short, which mainly gives that impression).

---

<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:** [January 24, 2018, 10:08am UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/13 "2018-01-24T10:08:02Z")

</div>

This sounds great Martin. Thanks for sharing

---

<div class="post-metadata">

**Author:** ![JorshTheHacker](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@JorshTheHacker](https://in-thread.sonic-pi.net/u/JorshTheHacker)\
**Post date:** [January 26, 2018, 3:56pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/14 "2018-01-26T15:56:29Z")

</div>

This is beautiful. Seriously.

---

<div class="post-metadata">

**Author:** ![JorshTheHacker](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@JorshTheHacker](https://in-thread.sonic-pi.net/u/JorshTheHacker)\
**Post date:** [January 26, 2018, 6:07pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/15 "2018-01-26T18:07:34Z")

</div>

I forked Martin’s randomness experiment, changing it to fit my need (I’m using it in a darker-sounding track), and also amplifying the randomness by a lot. Because the seed will only change once every beat, I added an increase/decrease by a random number (determined by the previous random seed). This, with the panning and the Prophet synth, leads to one of the trippiest riffs I have ever experienced. Period.

```
live_loop :ambient do
  t = Time.now.to_i + rrand_i(-1000000000000000000000000000000000000, 1000000000000000000000000000000000000)
  use_random_seed t
  puts "Time: #{t}"
  z = [58,60,63,66,68,70,72,74].choose
  n = ((chord :c, :m7) + [z]).sort
  v = (line 0.25, 1).reflect
  p = (line -0.85, 0.85, steps: 150).reflect
  with_fx :reverb, room: 0.75, mix: rrand(0,1) do
    s = synth :prophet, note: n.tick(:n), attack: [0,0,0,0,2].choose, release: [0.5,0.5,0.5,2].choose, amp: v.tick, pan: p.look
    s = synth :dark_ambience, note: n.tick(:n), attack: [0, 0.125, 0.25, 0.5, 0.625, 0.75, 0.825, 1].choose, release: [0.5,0.5,0.5,2].choose, amp: 1, pan: p.look, cutoff: 128, noise: [0,1,2,3,4].choose, ring: [rrand(0.1, 50), rrand(0.1, 50), rrand(0.1, 50)].choose
    control s, amp: v.tick, amp_slide: [0.0625, 0.25].choose, pan: p.look, pan_slide: 0.25, depth: 2, depth_slide: 1.5
  end
  sleep 0.25
end
```

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [January 26, 2018, 8:45pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/16 "2018-01-26T20:45:38Z")

</div>

Nice! For an even darker twist, try lowering the octave and applying the prophet’s built-in low pass filter randomly:

```ruby
live_loop :ambient do
  use_octave -2
  t = Time.now.to_i + rrand_i(-1000000000000000000000000000000000000, 1000000000000000000000000000000000000)
  use_random_seed t
  puts "Time: #{t}"
  z = [58,60,63,66,68,70,72,74].choose
  n = ((chord :c, :m7) + [z]).sort
  v = (line 0.25, 1).reflect
  p = (line -0.85, 0.85, steps: 150).reflect
  with_fx :reverb, room: 0.75, mix: rrand(0,1) do
    s = synth :prophet, note: n.tick(:n), attack: [0,0,0,0,2].choose, release: [0.5,0.5,0.5,2].choose, amp: v.tick, pan: p.look, cutoff: rrand(70, 120)
    s = synth :dark_ambience, note: n.tick(:n), attack: [0, 0.125, 0.25, 0.5, 0.625, 0.75, 0.825, 1].choose, release: [0.5,0.5,0.5,2].choose, amp: 1, pan: p.look, cutoff: 128, noise: [0,1,2,3,4].choose, ring: [rrand(0.1, 50), rrand(0.1, 50), rrand(0.1, 50)].choose
    control s, amp: v.tick, amp_slide: [0.0625, 0.25].choose, pan: p.look, pan_slide: 0.25, depth: 2, depth_slide: 1.5
  end
  sleep 0.25
end

```

---

<div class="post-metadata">

**Author:** ![JorshTheHacker](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@JorshTheHacker](https://in-thread.sonic-pi.net/u/JorshTheHacker)\
**Post date:** [January 26, 2018, 9:09pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/17 "2018-01-26T21:09:49Z")

</div>

I actually fiddled with it a bit further and did lower the octave, although I completely forgot the Prophet LPF. I used the :distortion FX as well to make it sound a bit more twisted, although I opted (_heh, heh._) to not use any randomization with the distortion level, simply for the sake of reducing load time.

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [January 26, 2018, 9:14pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/18 "2018-01-26T21:14:13Z")

</div>

> [@JorshTheHacker](#):
>
> simply for the sake of reducing load time.

What do you mean by ‘load time’? 🙂

---

<div class="post-metadata">

**Author:** ![JorshTheHacker](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@JorshTheHacker](https://in-thread.sonic-pi.net/u/JorshTheHacker)\
**Post date:** [January 26, 2018, 9:24pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/19 "2018-01-26T21:24:13Z")

</div>

I use an 8-year-old laptop and haven’t gotten around to upgrading/replacing it yet so it takes awhile to do pretty much anything, which includes loading FX/synths. The only reason I use it as opposed to my RPi 3B is because it has better audio quality (well, that and my nephew broke my USB keyboard…)

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [January 26, 2018, 9:31pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/20 "2018-01-26T21:31:27Z")

</div>

I’m still not quite sure what you mean by “loading FX/synths”. All the synthdefs are loaded at boot time, so there’s no loading going on. Only the samples are loaded on-the-fly (which you can pre-empt by using the `load_sample` fn).

When you say “loading FX/synths” what are you observing? Are you seeing timing issues?

---

<div class="post-metadata">

**Author:** ![JorshTheHacker](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@JorshTheHacker](https://in-thread.sonic-pi.net/u/JorshTheHacker)\
**Post date:** [January 26, 2018, 9:36pm UTC](https://in-thread.sonic-pi.net/t/more-real-randomization/608/21 "2018-01-26T21:36:52Z")

</div>

I misspoke (mis-typed?). As you say, the FX and synths are loaded at boot-time. I’ve been meaning to replace the audio card in my lappy for some time now (I got a custom one about 5 years ago and it’s starting to wear down between my SP usage and my Reaper usage), so it’s more likely than not just a hardware issue — the _rendering_ of the audio is what’s slow, I believe. I’ve been having this issue when playing tracks in Reaper as well, so it’s definitely not a problem with SP.

Sorry for the lack of clarity.

[Next page](https://in-thread.sonic-pi.net/t/more-real-randomization/608.md?page=2)
