Digital Dice

I want to create a program that lets me type in a number, and then the computer generates a random number between 1 and that number. I have no starting point other than

set maxNum to text returned of (display dialog "Enter in the highest possible number for the computer to \"roll.\"" default answer "6" default button 2

What should I do? Thanks! :slight_smile:

Hi,

AppleScript has a built-in random command


set maxNum to text returned of (display dialog "Enter in the highest possible number for the computer to \"roll.\"" default answer "6" default button 2)
try
	set maxNumInteger to maxNum as integer
	set randomNumber to random number from 1 to maxNumInteger
on error
	display dialog "Only integers" buttons {"Cancel"} default button 1
end try
randomNumber

Thanks, Stefan. After some tweaking (like changing the name from RandomNumber to RdmNum and adding a screen that showed what that was), I got this:

set maxNum to text returned of (display dialog "Enter in the highest possible number for the computer to \"roll.\"" default answer "6" default button 2)
try
	set maxNumInteger to maxNum as integer
	set RdmNum to random number from 1 to maxNumInteger
on error
	display dialog "Only integers" buttons {"Cancel"} default button 1
end try
display dialog "You rolled a " & RdmNum & "."

It works fine. Thanks! :slight_smile:

The 8-character variable name restriction is outdated for a long time :wink:

Lol, it just makes it easier for me to read.

Is there a simple way to change the buttons to say “Re-roll” and “Quit” after you roll and then, if you reroll, it starts you back up at the main screen, and if you click Quit, you exit? It would be nice. Thanks! :slight_smile:

Of course


repeat
	set maxNum to text returned of (display dialog "Enter in the highest possible number for the computer to \"roll.\"" default answer "6" default button 2)
	try
		set maxNumInteger to maxNum as integer
		set RdmNum to random number from 1 to maxNumInteger
	on error
		display dialog "Only integers" buttons {"Cancel"} default button 1
	end try
	display dialog "You rolled a " & RdmNum & "." buttons {"Quit", "Re-roll"} cancel button "Quit" default button 2
end repeat


Thanks stefan! It works fine. :slight_smile: