Hello,
In other programming languages there is a way to randomize the random seed using computer clock/timer, such as (in Visual Basic) we can use: randomize timer.
Is there anyway in Sonic Pi we can do this with “with_random_seed” where the seed is taken from computer clock/timer? At least the random sequence won’t be as predictable as the pseudorandom in Sonic Pi.
Strictly speaking, Time is not part of the Sonic Pi language per se.
Sonic Pi is built on top of Ruby, so some things from plain Ruby may still be available, but there are no guarantees that they will work, or if they do, whether they will continue to do so down the track. ‘Let the buyer beware’
I’v used this snippet a lot inside live_loops to ensure that things are random:
live_loop :some_loop do
SecureRandom.random_number(1000).times { rand }
# Do something else
end
This is just randomly consuming some random numbers. Not sure why, but I’v noticed that if you use only that “use_random_seed = Time.now.to_i” things are still not always random at least if you are using multiple threads.
Test this. It’s still pseudo random. If you repeat the random a few times, you’ll notice the pattern. Using Time.now.to_i is better, never repeat the same sequence twice.
SecureRandom.random_number(1000).times
loop do
print rand_i(100)
sleep 1
end
when I run this, my computer always gives: 75, 73, 46, 24, etc.
You are missing the { rand } from the end, which consumes the random numbers. If you try:
loop do
print SecureRandom.random_number(1000)
end
You can see that it produces random numbers. To consume random numbers use it with rand:
SecureRandom.random_number(1000).times { rand }
This calls rand 0-1000 times, and thus consumes some random numbers and in a way generated new subset of random numbers from certain seed. You can also use it as: