# Choose - Sleep with total of beat

**URL:** <https://in-thread.sonic-pi.net/t/choose-sleep-with-total-of-beat/7268>\
**Category:** Support, Help & Resources\
**Created:** [November 14, 2022, 7:44am UTC](https://in-thread.sonic-pi.net/t/choose-sleep-with-total-of-beat/7268 "2022-11-14T07:44:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tetsuo](https://avatars.discourse-cdn.com/v4/letter/t/e47774/32.png) [@Tetsuo](https://in-thread.sonic-pi.net/u/Tetsuo)\
**Post date:** [November 14, 2022, 7:44am UTC](https://in-thread.sonic-pi.net/t/choose-sleep-with-total-of-beat/7268/1 "2022-11-14T07:44:51Z")

</div>

Hi all,

I was wondering how you would do that, the best way:  
Here is a simple code:

live\_loop :test do  
16.times do  
play scale(:c4, :minor).choose  
sleep [0.125, 0.25, 0.5, 0.75, 1].choose  
end  
end

How can I do to be sure that the 16 times duration is always equal to 8 beat. I’d like the sum of the 16 “choose” always do 8

Any Idea?

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [November 14, 2022, 11:18am UTC](https://in-thread.sonic-pi.net/t/choose-sleep-with-total-of-beat/7268/2 "2022-11-14T11:18:40Z")

</div>

One option would be to make an array of 16 numbers that add up to 8, and shuffle them so that they play in a random order, but the total time will always be the same. Something like this maybe:

```ruby
live_loop :test do
  times = [1, 0.25, 0.25, 0.25, 0.75, 0.5, 0.5, 0.5] * 2
  times.shuffle.each do |t|
    play scale(:c4, :minor).choose
    sleep t
  end
end

```

---

<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:** [November 14, 2022, 10:03pm UTC](https://in-thread.sonic-pi.net/t/choose-sleep-with-total-of-beat/7268/3 "2022-11-14T22:03:31Z")

</div>

```ruby
loop do  
  play scale(:c4, :minor).choose, amp: choose([0,0,0,1])
  sleep 0.125
end
```

---

<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:** [November 14, 2022, 10:17pm UTC](https://in-thread.sonic-pi.net/t/choose-sleep-with-total-of-beat/7268/4 "2022-11-14T22:17:13Z")

</div>

clever solution. Perhaps better as a live loop so it can be used with other bits.  
I played both solutions (so far) together. Sounded nice.

```ruby
live_loop :test do
  times = [1, 0.25, 0.25, 0.25, 0.75, 0.5, 0.5, 0.5] * 2
  times.shuffle.each do |t|
    play scale(:c4, :minor).choose
    sleep t
  end
end

live_loop :test2 do  
  play scale(:c4, :minor).choose, amp: choose([0,0,0,1])
  sleep 0.125
end

```

---

<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:** [November 14, 2022, 11:16pm UTC](https://in-thread.sonic-pi.net/t/choose-sleep-with-total-of-beat/7268/5 "2022-11-14T23:16:46Z")

</div>

Thank You !!! ( Smiley Here )

---

<div class="post-metadata">

**Author:** ![Raul](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/raul/32/265_2.png) [@Raul](https://in-thread.sonic-pi.net/u/Raul)\
**Post date:** [November 15, 2022, 8:38am UTC](https://in-thread.sonic-pi.net/t/choose-sleep-with-total-of-beat/7268/6 "2022-11-15T08:38:52Z")

</div>

```ruby

a = [0.125,0.25,0.5,1]
t = 0
live_loop :a do
  play scale(:c4,:minor).choose
  sleep b = a.choose
  t +=b
  puts t
  if t >=15
    play scale(:c4,:minor).choose
    sleep 16-t
    t = 0
  end
  end

```

---

<div class="post-metadata">

**Author:** ![Tetsuo](https://avatars.discourse-cdn.com/v4/letter/t/e47774/32.png) [@Tetsuo](https://in-thread.sonic-pi.net/u/Tetsuo)\
**Post date:** [November 16, 2022, 2:41pm UTC](https://in-thread.sonic-pi.net/t/choose-sleep-with-total-of-beat/7268/7 "2022-11-16T14:41:45Z")

</div>

Thx for all your ideas
