Applescript help (again)

7 posts / 0 new
Last post
Offline
Last seen: 17 years 11 months ago
Joined: Nov 1 2004 - 08:41
Posts: 15
Applescript help (again)

I need some help here. I have an Applescript that's supposed to be a dice-roller - it can roll any number of dice of any side, and report the total. The only problem is, I can't get the random number to be an integer. Here is the script:

tell application "ircle 3.1 Am. English PPC"
set obj to argstring
set thenumber to 0
if obj = "" then
set r to display dialog "Number of dice?" default answer "1"
set obj to (text returned of r)
set t to display dialog "Number of sides?" default answer "6"
set obj2 to (text returned of t)
end if
set ch to currenttarget
set ecks to 0
repeat obj times
set roll to (random number from 1 to obj2)
set ecks2 to ecks + roll
end repeat
do "/me rolls " & obj & "d" & obj2 & " and gets " & ecks2 & "!"
end tell

The dialog box does not return an integer, which is why I think it's not working right. The results I get from rolling the default 1d6 come out to a 12 place decimal. -_-;

Anyone have any suggestions?

Offline
Last seen: 17 years 11 months ago
Joined: Nov 1 2004 - 08:41
Posts: 15
err, it seems to have added m

err, it seems to have added my topic twice. I apoligize for that.

Offline
Last seen: 17 years 11 months ago
Joined: Nov 1 2004 - 08:41
Posts: 15
okay well i figured something

okay well i figured something out. This is the part that's giving me trouble.

set ecks to 0
repeat obj times
set roll to (random number from 1 to obj2) -!-- this line sucks. it's not returning an integer
set ecks to ecks + roll -!-- saw an error there unrelated to my integer trouble
end repeat

Offline
Last seen: 17 years 11 months ago
Joined: Nov 1 2004 - 08:41
Posts: 15
Some more update

According to the scripting additions dictionary:
random number: Generate a random number
random number number -- the upper limit; if not specified, result is a real between 0 and 1
(from number) -- the lowest number to return
(to number) -- the greatest number to return (up to but not including)
(with seed number) -- a starting point for a repeatable sequence of random numbers
Result: number -- a real number between 0 and 1, or an integer between the from and to values provided

The last line states that if I have a from and to value, the random number command should return an integer. Why isn't it doing so?

Offline
Last seen: 17 years 11 months ago
Joined: Nov 1 2004 - 08:41
Posts: 15
ech

Okay, weird. Either the dialog boxes are not returning integers, or (random number between 1 and obj) is not returning an integer.

If the boxes are not returning integers, how do I fix that?

If the random number command is not returning an integer, how do I get "obj" to be an integer so that the random number command can return an integer and give me a real roll instead of a fraction of a roll?

Jon
Jon's picture
Offline
Last seen: 12 years 11 months ago
Joined: Dec 20 2003 - 10:38
Posts: 2804
I don't know anything about A

I don't know anything about AppleScript, but I've dealt with the same problem in other languages. The random number is a real between 0 and 1. Typically I would then multiply by 10 or 100, depending on the range I needed. Then a language usually lets you specify using either the integer or fractional part of a number. So, take an example:

int obj = ipart ( random * 10 )

Would return a random number from 0 to 9. Using 100 instead of 10 would give a number from 0 to 99. A quick test fo the number to see if it is in range of your dice, and then spit it out to the app. If it's outta the range, roll again.

I dunno how the optional range settings of the AS random number generator work, but you should be able to accomplish something similar. The main part is taking the interger part of the number, and dropping the fractional part after doing a bit of setup to get it near your range.

Offline
Last seen: 17 years 11 months ago
Joined: Nov 1 2004 - 08:41
Posts: 15
Yeah, I know this is old

AppleScript has a command I found called "round". I can round the number up, down, left, right, standard (up if it's .5 or higher) and so on.
So I got it to work, and after much testing I discovered it did work properly, giving me numbers from 1 to the highest number on the dice I was rolling.
If anyone would like a copy of the script, I wouldn't mind sharing it. Drop me a line or something, and I'll be happy to hook you up. Smile

Log in or register to post comments