# Force random notes into a scale after transposing

**URL:** <https://in-thread.sonic-pi.net/t/force-random-notes-into-a-scale-after-transposing/4493>\
**Category:** Support, Help & Resources\
**Created:** [October 12, 2020, 8:09am UTC](https://in-thread.sonic-pi.net/t/force-random-notes-into-a-scale-after-transposing/4493 "2020-10-12T08:09:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![simcitizen](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/simcitizen/32/1609_2.png) [@simcitizen](https://in-thread.sonic-pi.net/u/simcitizen)\
**Post date:** [October 12, 2020, 8:09am UTC](https://in-thread.sonic-pi.net/t/force-random-notes-into-a-scale-after-transposing/4493/1 "2020-10-12T08:09:06Z")

</div>

Hi everybody, Hi Sam,

I’m relatively new on Sonic Pi, so first let me thank Sonic Pi team and Sam Arron for making such a cool program! I’m having so much fun on it!

So here’s my question: I’m working on a project wich uses generating random notes and transposing to make variations. Is there a way to force the transposed result to respect a scale I defined before, such as in the “scale” midi effect in ableton?

Hope my question is clear, and many thanks in advance!

---

<div class="post-metadata">

**Author:** ![simcitizen](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/simcitizen/32/1609_2.png) [@simcitizen](https://in-thread.sonic-pi.net/u/simcitizen)\
**Post date:** [October 12, 2020, 8:30am UTC](https://in-thread.sonic-pi.net/t/force-random-notes-into-a-scale-after-transposing/4493/2 "2020-10-12T08:30:20Z")

</div>

I don’t know if my question is clear enough, so let me try to aks it a different way:

imagine I have a note X wich is randomly generated from different variables and decisions I made in the code, for example a E, but I would like this one to respect a scale of (still for example) C minor, so I get E flat instead.

How is that possible in Sonic Pi?

Again sorry if my question is dumb and the answer evident, I’m a newbie to Sonic Pi

---

<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:** [October 12, 2020, 9:22am UTC](https://in-thread.sonic-pi.net/t/force-random-notes-into-a-scale-after-transposing/4493/3 "2020-10-12T09:22:36Z")

</div>

Not quite sure of your question, but you can take a random integer and use it to index a list of allowed notes.  
For example, you could have a three octave range of notes in the scale of :c and play notes from this chosen randomly like this.

```ruby
notes = scale(:c3,:major,num_octaves: 3) # list of 22 notes
live_loop :test do
  n=rand_i(22)
  puts n
  play notes[n]
  sleep 0.2
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:** [October 12, 2020, 9:26am UTC](https://in-thread.sonic-pi.net/t/force-random-notes-into-a-scale-after-transposing/4493/4 "2020-10-12T09:26:40Z")

</div>

Hi @simcitizen - welcome to our community!

Fab question! I don’t have much to add at this point in addition to @robin.newman’s remarks other than to say that it might be easier to think of a random _nth_ note of a scale rather than, say, `E`. That way you get to look up the _nth_ note using the approach Robin demonstrates above.

Good luck and I look forward to hearing more of what you get up to with Sonic Pi!

---

<div class="post-metadata">

**Author:** ![simcitizen](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/simcitizen/32/1609_2.png) [@simcitizen](https://in-thread.sonic-pi.net/u/simcitizen)\
**Post date:** [October 12, 2020, 10:25am UTC](https://in-thread.sonic-pi.net/t/force-random-notes-into-a-scale-after-transposing/4493/5 "2020-10-12T10:25:26Z")

</div>

Thanks Robin and Sam for your quick replies! You totally answered to my question! I’m going to immediatly test your suggestions!
