Randomize the order of 4 looping samples, no sample is chosen twice

I have 4 samples with the same length (32 bars). I want to start with one sample playing, then adding one by one. Once a sample is chosen and playing, it is looping. My goal is to randomize the order of the samples whereas no sample is chosen twice so that after 4 rounds, all samples are sounding. As simple as that.

Since I want the code to decide with which sample to start, I am working with if / else statement to narrow the choices after each round. However, for some reason the code still sometimes chooses the same samples.
Where is the mistake?

I know it is a lot of options, maybe there is a simpler way to do it?

Thank you!


sounds = "/samples"
r = Random.rand(1..10)
use_random_seed r

sound1 = sounds, ["a.wav", "b.wav", "c.wav", "d.wav"].choose

sound2 = sounds, ["a.wav", "b.wav", "c.wav"].choose
sound3 = sounds, ["d.wav","b.wav", "c.wav"].choose
sound4 = sounds, ["d.wav", "a.wav", "b.wav"].choose
sound5 = sounds, ["d.wav", "a.wav", "c.wav"].choose
sound6 = sounds, ["b.wav", "c.wav"].choose
sound7 = sounds, ["a.wav", "c.wav"].choose
sound8 = sounds, ["a.wav", "b.wav"].choose
sound9 = sounds, ["d.wav", "c.wav"].choose
sound10 = sounds, ["b.wav", "d.wav"].choose
sound11 = sounds, ["a.wav", "d.wav"].choose

live_loop :loop1 do
  sample sound1, amp:4, pan: -1
  sleep 32
end

sleep 32

if sound1 == "/samples/d.wav"
  live_loop :loop2 do
    sample sound2, amp:4, pan: 1
    sleep 32
  end
  
elsif sound1 == "/samples/a.wav"
  live_loop :loop2 do
    sample sound3, amp:4, pan: 1
    sleep 32
  end
  
elsif sound1 == "/samples/c.wav"
  live_loop :loop2 do
    sample sound4, amp:4, pan: 1
    sleep 32
  end
  
else
  live_loop :loop2 do
    sample sound5, amp:4, pan: 1
    sleep 32
  end
end


sleep 32

if sound1 == "/samples/d.wav" && sound2 == "/samples/a.wav" or sound2 == "/samples/d.wav" && sound1 == "/samples/a.wav"
  live_loop :loop3 do
    sample sound6, amp:4
    sleep 32
  end
  
elsif sound1 == "/samples/d.wav" && sound2 == "/samples/b.wav" or sound2 == "/samples/d.wav" && sound1 == "/samples/b.wav"
  live_loop :loop3 do
    sample sound7, amp:4
    sleep 32
  end
  
elsif sound1 == "/samples/d.wav" && sound2 == "/samples/c.wav" or sound2 == "/samples/d.wav" && sound1 == "/samples/c.wav"
  live_loop :loop3 do
    sample sound8, amp:4
    sleep 32
  end
  
elsif sound1 == "/samples/a.wav" && sound2 == "/samples/b.wav" or sound2 == "/samples/a.wav" && sound1 == "/samples/b.wav"
  live_loop :loop3 do
    sample sound9, amp:4
    sleep 32
  end
  
elsif sound1 == "/samples/a.wav" && sound1 == "/samples/c.wav" or "/samples/a.wav" && sound1 == "/samples/c.wav"
  live_loop :loop3 do
    sample sound10, amp:4
    sleep 32
  end
  
else
  live_loop :loop3 do
    sample sound11, amp:4
    sleep 32
  end
end

hi

what about using two arrays : a full one including all your samples and a copy of this one from you delete the sample already played ? As that you only have to choose from the copy array by a pick.

just an idea maybe not formidable at all :slight_smile:

sounds = "/samples"
r = Random.rand(1..10)
use_random_seed r

sounda = a.wav
soundb = b.wav
soundc = c.wav
soundd = d.wav

sounds = [sounda,soundb,soundc,soundd]

live_loop :loop1 do
  sounds_rand = sounds.shuffle
  4.times do
    sample sounds_rand.tick, amp:4, pan: -1
    sleep 32
  end
  
end```
1 Like

can’t you just use sounds = [sounda,soundb,soundc,soundd].shuffle
This will randomise the order of teh sounds. However with SP you will get the same result each time.
You can use a random seed before this if you want it to be different each time.