For this bit
set %flip $rand(1,2)
I would just use var as there is no need to set a global variable for this one.
var %flip $rand(1,2)