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?

Posted on Apr 12, 2019 11:12 AM

Reply
3 replies

Apr 12, 2019 3:25 PM in response to sini176

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


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

applescript random number between -20 and 20 except 0

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.