Apple Script Random Number Generator 6
Apple Script
Hey
Having started to mess about in automator, I wanted to mess about more in Apple Script. This little trick will hopefully teach you how to generate a random number and display it in a message box. The first step is to open Apple Script Editor. Go to Applications > Apple Script > Script Editor.
You are now on the code screen. You can now enter the random number generator piece of code. This is defined by random number. Insert this into the code into the code view. Your random number generator is now done. At the moment it isn’t very useful.
To make the random number generator a bit more useful we are going to display this number through a dialog box. First we need to set the variable for the random number and then display it through the use of a dialog box. The give up after 2 will automatically make the box disappear.
set rn to (random number) as text
display dialog rn giving up after 2
You can of course change the variables for the random number. At the moment it will display a number between 0 and 1. This can be changed using the following syntax.
random number from 1 to 10
The first number can also be a minus as well. This will of course generate some minus numbers as well. Like with any random number generator you can use a seed value. Usually most random number generator will use either a CPU cycle number or the time to start the number generation. You can use a starting seed value for the random number. This is done through the syntax:
random number from -10.0 to 10 with seed 12
You can now generate random numbers all you like. For a final touch you can add a variable so the random number is copied to the clipboard. This is really simple you just add the following piece of code to the end of the code.
set the clipboard to rn
The final code now looks like this.
set rn to (random number from 1 to 10 with seed 2) as text
set the clipboard to rn
display dialog rn giving up after 2
If you know any more random number generation techniques with Apple Script please leave a comment below.
If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.
Where To Next?
6 Responses to “Apple Script Random Number Generator”
-
1
Is there a way that you can manually pick a set of numbers to be randomly generated into certain combinations?
ThanksComment By Jon on July 17th, at 5:00 am
-
2
Not that I know of. I am not a Apple Script buff. It can be done for sure. Google will likely be your friend.
Comment By admin on July 17th, at 10:52 am
-
3
Having not used Apple Script, but being a web developer who has worked with many code languages, my first thought would be to populate an array with the numbers you want, then call a random index in that array with the random number.
Problem is that I don’t know if Apple Script has arrays…
Comment By Brad on August 6th, at 3:45 pm
-
4
Is there a way to make it include decimals with the number.
I am relatively new to coding especially with apple script.
Thanks in advanceComment By GG on October 14th, at 8:57 pm
-
5
Best way I have found is to do this code.
(random number) * (random number from 1 to 10)It gets your random numbers with decimals but it is also has a number in front of the decimal. So your result would be something similar to this: 3.259870233843 etc.
Hope that helps.
Comment By admin on October 14th, at 10:06 pm
-
6
Yeah that helped a lot thanks!
Comment By GG on October 15th, at 5:27 pm

