# Randomisation for a group of samples

**URL:** <https://in-thread.sonic-pi.net/t/randomisation-for-a-group-of-samples/5336>\
**Category:** Support, Help & Resources\
**Created:** [March 29, 2021, 5:23pm UTC](https://in-thread.sonic-pi.net/t/randomisation-for-a-group-of-samples/5336 "2021-03-29T17:23:52Z")\
**Posts on this page:** 1\
**Showing post:** 5

<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:** [March 30, 2021, 8:56am UTC](https://in-thread.sonic-pi.net/t/randomisation-for-a-group-of-samples/5336/5 "2021-03-30T08:56:51Z")

</div>

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:

```ruby
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:

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

```

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

```ruby
sample [:elec_triangle, :tabla_tas1].choose

```

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

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:

```ruby
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:

```ruby
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:

```ruby
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:

```ruby
sample sample_names(:vinyl).choose

```

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

```ruby
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 🙂

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](https://sonic-pi.net/tutorial#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](https://in-thread.sonic-pi.net/t/ticking-through-samples-randomly-from-outside-folder/1553/2)

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!

---

_[View the full topic](https://in-thread.sonic-pi.net/t/randomisation-for-a-group-of-samples/5336)._
