About if rand with a drum sample

Hi there, here is a extract from a Sam’s patch:

live_loop :drums do
  16.times do
    sample :bd_haus, rate: 2, cutoff: 110 if rand < 0.20
    sleep 0.125
  end
end

I try to understand pecisely what the “rand < 0.20” makes here. Okay i understand very well the result (makes me think to a kind of “if spread”) but: "If "creates a boolean (true, false) and so, what is the relation between the last number (here 0.20) and the boolean…tell me if my interpretation is correct because this simple line of code is great and very interesting…Thanks for help! :wink:

rand returns a random float between 0 and 1, which is compared to 0.2. The if sees the resulting boolean.

It’s an alternative to if one_in(5) , the 0.2 is a percentage.

1 Like

rand returns a random number between 0 and 1 so this is evaluating
is eg 0.345 < 0.2 The answer is either yes it is true or no it isn’t false

You could amend the loop to store the rand value in the variable r and then print the condition giving true or false as below

live_loop :drums do
  16.times do
    r = rand
    puts (r < 0.2) #prints true or false
    sample :bd_haus, rate: 2, cutoff: 110 if r < 0.20
    sleep 0.125
  end
end
1 Like

@robin.newman @Davids-Music-Lab Thanks David and Robin, it’s perfectly clear now! :grinning: :+1: