Force integer from a (range)

Hi
back again for more noobish questions - is there a method for forcing (range 0, 10, 1) to return an integer please; I’m trying to index through a folder of samples, have already LMGTFY

Love
Brendan

a = [0,1,2,3]

live_loop :notes do
  sample glocken, a.tick
  sleep 2
end

I have NO idea why SPi was complaining about floats earlier - probably the difference between an array of explicit integers, and a ring/range returning floats. My bad.

It seems that Sonic Pi’s range function always returns floats, even when all the arguments are integers, so using range here will fail. You can work around it by converting the result to an integer like this:

a = (range 0, 4)

live_loop :notes do
  sample glocken, a.tick.to_i
  sleep 2
end

But that’s not ideal, as you have to remember to do the conversion - it would be nicer if range would return integers when it was given integer parameters.

However, now that I think about it, you don’t really need range here at all, since tick is already counting up, and you could use % to limit how high it gets before it wraps around, so this should do the same thing:

live_loop :notes do
  sample glocken, tick % 4
  sleep 2
end
1 Like

Thanks for the helpful tips!
.to_i is what I was looking for, but as you say, there are other, better, ways to do this

Brendan

1 Like