applescript random number between -20 and 20 except 0
hello all,
for my applescript i want to set a variable x to a random number between -20 and 20 excluding 0, how is that possible?
hello all,
for my applescript i want to set a variable x to a random number between -20 and 20 excluding 0, how is that possible?
Use the following:
set x to 0
repeat until x is not 0
set x to random number from -20 to 20
end repeat
(170268)
Will depend on how much of a purist you want to be. For instance, excluding 0 when found.
hiroto gives a good idea here:
https://discussions.apple.com/thread/7256911
some number in {-20, ... -1, 1, ... ,20} no applescript doesn't support ... . You know the drill.
https://stackoverflow.com/questions/39514468/applescript-random-number-between
R
This will generate a random number between a lower range (LR) and an upper range (UR) exclusive of zero.
set LR to -20 as integer
set UR to 20 as integer
set args to LR & space & UR
set x to get_random(args)
display dialog x as text
return
on get_random(args)
return do shell script "ruby <<-EOF - " & args & "
#!/usr/bin/ruby
lr, ur = ARGV.map { |e| Integer(e) }
# generate a randon number between lr and ur bounds excluding 0
puts rand(lr..ur).nonzero?
EOF"
end get_random
applescript random number between -20 and 20 except 0