# How to hear the different kind of random\_sources

**URL:** <https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210>\
**Category:** Creations & Ideas\
**Created:** [September 19, 2024, 1:56pm UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210 "2024-09-19T13:56:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![beryann](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/beryann/32/3133_2.png) [@beryann](https://in-thread.sonic-pi.net/u/beryann)\
**Post date:** [September 19, 2024, 1:56pm UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/1 "2024-09-19T13:56:11Z")

</div>

Hello! The idea came to me when I was looking at Mr Bomb’s video @mrbombmusic about random\_source. I wanted to _hear_ really the differences between them. So I wrote this inspired by his tutorial and it works, so if you see something to improve, it will be with pleasure… 🤓

```ruby
use_bpm 95

use_random_source :pink 
##change with perlin, etc

a = []

with_fx :reverb, mix: 0.8 do
  200.times do
    n = rand
    a.push(n)
    play n * 150, amp: 0.5
    sleep 0.25
  end
end

puts a

```

---

<div class="post-metadata">

**Author:** ![HarryLeBlanc](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/harryleblanc/32/2813_2.png) [@HarryLeBlanc](https://in-thread.sonic-pi.net/u/HarryLeBlanc)\
**Post date:** [September 25, 2024, 4:32pm UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/2 "2024-09-25T16:32:26Z")

</div>

Very interesting and useful!  
Here’s a tweaked version that steps through them all:

```ruby
use_bpm 95

randomsources = [:white, :pink, :perlin, :light_pink, :dark_pink]
use_random_source :pink
##change with perlin, etc

a = []

randomsources.each do |x|
  use_random_source x
  with_fx :reverb, mix: 0.8 do
    40.times do
      n = rand
      a.push(n)
      play n * 150, amp: 0.5
      sleep 0.25
    end
  end
  
  puts a
  sleep 2
end #each

```

I especially like perlin and dark\_pink for melodies that don’t jump around too much.  
Is there a clever way to constrain these random notes to a given scale?

---

<div class="post-metadata">

**Author:** ![beryann](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/beryann/32/3133_2.png) [@beryann](https://in-thread.sonic-pi.net/u/beryann)\
**Post date:** [September 25, 2024, 4:47pm UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/3 "2024-09-25T16:47:51Z")

</div>

Good Harry @HarryLeBlanc and thanks for sharing your amelioration of my initial patch…It has a better look now! 😀  
And it’s right, you can use one of them for a compostion but I find fascinating the idea in itself to be able to hear all these “discret” numbers, so small and inaudible at the beginning!

---

<div class="post-metadata">

**Author:** ![robst247](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/robst247/32/2799_2.png) [@robst247](https://in-thread.sonic-pi.net/u/robst247)\
**Post date:** [January 5, 2025, 2:41pm UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/4 "2025-01-05T14:41:38Z")

</div>

Hi Harry,

Please explain what a.push(n) does?

Best regards,  
Rob

---

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [January 6, 2025, 10:53am UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/5 "2025-01-06T10:53:35Z")

</div>

Hi @robst247

n is a random float between 0 and 1, a is an initially empty array, and a.push(n) simply appends a new random float to that array:

```ruby
use_bpm 95

use_random_source :pink
##change with perlin, etc

a = []
puts a
sleep 1

5.times do
  n = rand
  a.push(n)
  puts n
  play n * 150, amp: 0.5
  sleep 1
end

puts a

```

HTH

PD-Pi

---

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [January 6, 2025, 10:55am UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/6 "2025-01-06T10:55:17Z")

</div>

[https://ruby-doc.org/core-2.7.0/Array.html#method-i-push](https://ruby-doc.org/core-2.7.0/Array.html#method-i-push)

```ruby
a = [1,2,3]
puts a
sleep 1

a = a.push(17,-42,"bla")
puts a

```

---

<div class="post-metadata">

**Author:** ![robst247](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/robst247/32/2799_2.png) [@robst247](https://in-thread.sonic-pi.net/u/robst247)\
**Post date:** [January 6, 2025, 12:54pm UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/7 "2025-01-06T12:54:08Z")

</div>

> [@brendanmac](#):
>
> ```ruby
> use_bpm 95
> 
> use_random_source :pink
> ##change with perlin, etc
> 
> a = []
> puts a
> sleep 1
> 
> 5.times do
> n = rand
> a.push(n)
> puts n
> play n * 150, amp: 0.5
> sleep 1
> end
> 
> puts a
> 
> ```

Crystal clear - thanks 🙂  
Where does the ‘push’ function come from?  
I’ve not seen it before, and it’s not described in Sonic Pi help.  
Is this from Ruby?

Cheers, Rob

---

<div class="post-metadata">

**Author:** ![robst247](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/robst247/32/2799_2.png) [@robst247](https://in-thread.sonic-pi.net/u/robst247)\
**Post date:** [January 6, 2025, 1:17pm UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/8 "2025-01-06T13:17:24Z")

</div>

I’ve found the answer:

> **[Ruby | push() function - GeeksforGeeks](https://www.geeksforgeeks.org/ruby-push-function/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

It simply appends elements to a pre-defined array.

Now I’m wondering how to quantize a series of random floats in an array to a musical scale and also how to impose lower and upper limits, in order to create a random-note sequencer.

---

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [January 6, 2025, 2:05pm UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/9 "2025-01-06T14:05:53Z")

</div>

Hi  
look for “scale” in the tutorial and also in the Help/Lang document. One can get tied up in knots using ‘custom’ arrays or rings for constrained random melodies. Sonic Pi provides many useful functions for creating such patterns.

Look at the examples (and mr bomb’s response) in this post:

> [@Constrained 'random' melody generator](https://in-thread.sonic-pi.net/t/constrained-random-melody-generator/8763/8):
>
> Hi @mrbombmusic and thanks for replying (and even bigger thanks for your indispensable tutorial series too!!). I will def check out those two links, but I am aware of the repetitive results when trying to randomise a random seed (after watching and digesting every single one of your videos) smiley . And I chose the upper bound 50 because it gives me ‘some’ randomisation, any lower and the random seed sticks in predictable ways. Is using Time.now computationally expensive? I’ll give it a ratt…

PD-Pi

---

<div class="post-metadata">

**Author:** ![robst247](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/robst247/32/2799_2.png) [@robst247](https://in-thread.sonic-pi.net/u/robst247)\
**Post date:** [January 7, 2025, 9:50am UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/10 "2025-01-07T09:50:39Z")

</div>

Thanks for these suggestions. I am aware of them and have used them in my own Sonic Pi coding, but are they truly random or are they pseudo-random (i.e. with some repetition)? I’m still intrigued by the possibility of scale-quantising truly random series of floating point numbers; however, I have not yet conceived of a means of achieving this.

---

<div class="post-metadata">

**Author:** ![brendanmac](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/brendanmac/32/2809_2.png) [@brendanmac](https://in-thread.sonic-pi.net/u/brendanmac)\
**Post date:** [January 7, 2025, 10:11am UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/11 "2025-01-07T10:11:28Z")

</div>

Hi  
I’m sure you’ve done this already, but if not, try a keyword search in this forum - I am certain the topic has come up before in various contexts.

HTH

PD-Pi

---

<div class="post-metadata">

**Author:** ![HarryLeBlanc](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/harryleblanc/32/2813_2.png) [@HarryLeBlanc](https://in-thread.sonic-pi.net/u/HarryLeBlanc)\
**Post date:** [January 10, 2025, 3:03am UTC](https://in-thread.sonic-pi.net/t/how-to-hear-the-different-kind-of-random-sources/9210/12 "2025-01-10T03:03:18Z")

</div>

I came up with a method to find the closest note to a scale. Here it is. (It relies on a function, cleanchordorscale, which can be found in my library YummyFillings.rb. It’s on github, under my username, HarryLeBlanc.)

```ruby
eval_file "e:/yummy/yummyfillings.rb"

define :closestnote do |thisnote, thisscale|
  thisscale = cleanchordorscale thisscale
  #debugprint thisscale
  foundnote = nil
  thisstep = 0
  thisneg = true
  while !foundnote
    thissign = (thisneg ? -1 : 1)
    #debugprint "test value: ", thisnote + (thisstep * thissign)
    if thisscale.include? thisnote + (thisstep * thissign)
      #debugprint "got a match!"
      foundnote = thisnote + (thisstep * thissign)
    elsif thisneg
      #debugprint "got a negative, incrementing step"
      thisstep += 1
      #debugprint "thisstep: :", thisstep
    end #if foundnote or thisneg
    thisneg = !thisneg
    #debugprint "thisneg: ", thisneg
  end #while
  return foundnote
end #define closestnote

thisscale = scale(:a0, :minor, num_octaves: 6)

use_random_seed Time.now.to_i
thisnote = rand_i(89) #full piano range
debugprint "thisnote: ", thisnote

debugprint "closestnote: ", closestnote(thisnote, thisscale)

```

Hope you find this useful. Should allow you to constrain random integers to any arbitrary scale or array of notes.  
I think I’ll add it to YummyFillings.rb for the next release.  
Cheers,  
H
