Randomisation for a group of samples

Hi :slight_smile:
Is it possible to select a folder or group of samples from the sonic pi sample library, for example, ambient sounds, or electric sounds, or a group of samples you have created yourself on your laptop, and set it to a random select?

sounds=[:elec_triangle, :tabla_tas1]

loop do
  sample choose(sounds)
  sleep 1
end

Someone asked a similar question… I just searched random samples, and found this:

Eli…

Hey hey @Monrhea :slight_smile:
The others here have already pointed out various ways of random selection - do go ahead and ask if you have any further questions about that of course.
Additionally, you can get programmatically get a list of all the built-in sample categories with sample_groups, and all the samples within a particular category with sample_names(category).
Hope that helps! :+1:

Hi @Monrhea,

the previous answers on here have highlighted a variety of approaches to randomisation, but I thought that it might be useful to summarise them all here with gentle introductions to each - just in case that’s useful for you and others that are reading this in the future.

Firstly, @hitsware showed us that a very generic approach to choosing a random thing from a list of things is pretty easy. Firstly you need your list of things to choose from. Say: [1, 2, 3, 4, 5], then to choose a random item from that list you just need to add .choose which will pick you one at random. This is kind of like having a scrabble bag of letters, popping your hand in and pulling one out.

Let’s take a look:

print [1, 2, 3, 4, 5].choose

This will print either 1, 2, 3, 4, or 5. It’s important to realise that this could be any item in the list and you can do anything with the result of choose. So, you could use this value as a sleep time:

sleep [1, 2, 3, 4, 5].choose

Or if your list is the names of samples, you can play one of those:

sample [:elec_triangle, :tabla_tas1].choose

This will only play one of the two options, not both at the same time :slight_smile:

Note that @hitsware also used a variable which definitely can help if you want to use your list in multiple places or want to keep your lines of code shorter:

my_samps = [:elec_triangle, :tabla_tas1]
sample my_samps.choose

So, this would work if you know the names of the samples you’d like to randomly work with. This will work with any sample name across any sample set.

However, your question talks about existing groups of samples such as the ambient or electric sounds. This is where @ethancrawford’s suggestions help. Sonic Pi has a built in function called sample_groups which will return a list (actually a ring, but the distinction here isn’t too important) of all the groups of the built-in samples:

print sample_groups

Will print the following to the log: (ring :ambi, :bass, :bd, :drum, :elec, :glitch, :guit, :loop, :mehackit, :misc, :perc, :sn, :tabla, :vinyl). We can now use one of these names with the function sample_names to access a list of all the samples in that group:

print sample_names(:vinyl)

Which will print out: (ring :vinyl_backspin, :vinyl_hiss, :vinyl_rewind, :vinyl_scratch).

We can therefore combine both @ethancrawford’s answer with @hitsware’s to .choose a random sample from a given group:

sample sample_names(:vinyl).choose

We could even go further and choose a random sample from a subset of groups:

groups = [:vinyl, :glitch]
rand_group = groups.choose
sample sample_names(rand_group).choose

Here, we’re choosing a random group from either :vinyl or :glitch and using that to get all the sample names and then choosing one of those :slight_smile:

Finally, when you’re working with your own samples, you can use the powerful sample searching and pattern matching tools built into Sonic Pi’s sample function. Take a look at the documentation for sample for a very thorough explanation of what’s possible. Section 3.7 of the tutorial also has some detailed explanation of this. Of course, there’s also my previous answer to a related question that @Eli kindly linked to which would be an excellent way of starting to learn about this: Ticking through samples randomly from outside folder - #2 by samaaron

I really hope this helps you get started. Please let us know if you have any further questions. This stuff can be a bit tricky until it clicks in your brain, so be patient and I know you’ll be able to get it eventually!

3 Likes

Another option is playing samples in random order, without repetition. This can be achieved using shuffle on the list of samples:

samples = sample_names(:ambi).shuffle

for s in samples
  sample s
  sleep sample_duration s
end

Hey all, this seems like the right place to chip in with this.

I tried to get a random group returned, using sample_groups.choose but always get :perc

I tried generating a random seed to get a new random group

use_random_seed 4
sam = sample_names (sample_groups.choose)

This worked as expected :+1:
Does anyone ever dabble with more/truly random performances?
Like generating a random seed from an integer range, or a randomly generated integer.

Whilst using SonicPi’s rand with use_random_seed generates same seed on each run

use_random_seed (rand 100000000000000000)
puts current_random_seed
sam = sample_names (sample_groups.choose)
puts sam # (ring :glitch_bass_g, :glitch_perc1, :glitch_perc2, :glitch_perc3, :glitch_perc4, :glitch_perc5, :glitch_robot1, :glitch_robot2)

Using the Random.rand function generates a random result each time

use_random_seed (Random.rand 100000000000000000)
puts current_random_seed
sam = sample_names (sample_groups.choose)
puts sam

If you did want more randomness, if this the way to go?
Can similar be achieved using SP’s methods?

I tried a couple of things, like shuffling and choosing a group from a variable, but without new seeds results are reproduced.

use_random_seed Time.now.to_i will give you a different selection for .choose