Apple Script Random Number Generator 6

Apple Script

Apple Script Random Number Generator


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?

  • Subscribe To Mac Tricks And Tips