# Name of a sample that has been randomly picked up

**URL:** <https://in-thread.sonic-pi.net/t/name-of-a-sample-that-has-been-randomly-picked-up/9354>\
**Category:** Support, Help & Resources\
**Created:** [December 4, 2024, 11:05am UTC](https://in-thread.sonic-pi.net/t/name-of-a-sample-that-has-been-randomly-picked-up/9354 "2024-12-04T11:05:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@Fred](https://in-thread.sonic-pi.net/u/Fred)\
**Post date:** [December 4, 2024, 11:05am UTC](https://in-thread.sonic-pi.net/t/name-of-a-sample-that-has-been-randomly-picked-up/9354/1 "2024-12-04T11:05:32Z")

</div>

Hello everybody,

I am writing instructions to randomly choose a sample into a file collection. Once it has been chosen, I would like to store the name of this file in a variable called : Name.  
I see the name of the file in the console but I can’t figure out how to get hold of it in my code.  
This is what I have written so far :  
collection = “/home/fred/Desktop/tracks/”  
use\_random\_seed Time.now.to\_i  
Rr = rrand\_i(1,120)  
sample collection, Rr  
sleep 1  
Puts = collection, Rr

It returns this in the console :

# {run: 1, time: 0.0}

# └─ sample “~/Desktop/tracks”,

# “033-ZanMar-Mel3-AC&.wav”

#{run: 1, time: 1.0}  
#└─ “/home/fred/Desktop/tracks/” 21

But I can’t find a way to write the complete filename “033-ZanMar-Mel3-AC&.wav” in my Name variable.

Can someone have a tip ?

Best regards,

Fred

---

<div class="post-metadata">

**Author:** ![R2L](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/r2l/32/2508_2.png) [@R2L](https://in-thread.sonic-pi.net/u/R2L)\
**Post date:** [December 4, 2024, 11:11am UTC](https://in-thread.sonic-pi.net/t/name-of-a-sample-that-has-been-randomly-picked-up/9354/2 "2024-12-04T11:11:13Z")

</div>

```ruby
sample_name = collection, Rr # store reference to sample
puts sample_name # output sample name
sample sample_name # play sample

```

should do it

---

<div class="post-metadata">

**Author:** ![Fred](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@Fred](https://in-thread.sonic-pi.net/u/Fred)\
**Post date:** [December 4, 2024, 12:47pm UTC](https://in-thread.sonic-pi.net/t/name-of-a-sample-that-has-been-randomly-picked-up/9354/3 "2024-12-04T12:47:41Z")

</div>

Thanks R2L,  
But unfortunately that doesn’t help here.  
The output of  
puts sample\_name  
is

# {run: 2, time: 1.0}

└─ [“/home/fred/Desktop/tracks/”, 5]  
That is “file path to collection” + Rr"  
But not what I am looking for : the real sample name.  
My sample names show the type of instrument and the key in which they are playing.  
I want to use this information to overlap with other samples.  
Best regards,  
Fred

---

<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:** [December 4, 2024, 12:56pm UTC](https://in-thread.sonic-pi.net/t/name-of-a-sample-that-has-been-randomly-picked-up/9354/4 "2024-12-04T12:56:05Z")

</div>

Try this:

```ruby
sample_name = (sample_paths collection, Rr)[0]
puts sample_name
sample sample_name

```

`sample_paths` takes the same arguments as `sample` but returns a ring of paths. The `[0]` on the end is to get the first (and only) element of that ring, since in this case it will always be a ring of length one.

---

<div class="post-metadata">

**Author:** ![Fred](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@Fred](https://in-thread.sonic-pi.net/u/Fred)\
**Post date:** [December 4, 2024, 1:12pm UTC](https://in-thread.sonic-pi.net/t/name-of-a-sample-that-has-been-randomly-picked-up/9354/5 "2024-12-04T13:12:18Z")

</div>

Thank you for your reply, emlyn,  
This is the solution.  
Now that I have the full file name, I will be able to build on top of it to launch the next samples.  
Great 😉
