Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Simple dice script?

Skell Dagger
Smitten
Join date: 26 Jun 2007
Posts: 1,885
05-11-2008 15:44
I'm looking for a script that can be placed into a single prim to be rezzed and used as a very simple dice for roleplaying. When the prim is touched by any avatar, the script would detect the avatar's name and return a random number from 1-50, thus:

Avatar A touches the prim. Response: "Avatar A rolls 36"

Avatar B touches the prim. Response: "Avatar B rolls 16"

I'm probably asking for the most simple script ever, but I'm a complete newbie when it comes to scripting, so any help would be greatly appreciated. I know there are plenty of dice available out there, with all the typical variants that tabletop gamers are used to, but I'd like to put this in a free beginner's roleplaying kit, thus the simpler the better.

Many thanks in advance, if you can help!
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
05-11-2008 15:47
*cough* One die; two dice.

Anyway.

CODE


default
{
touch_start(integer n)
{
llSay(0, llDetectedName(0) + " rolls " + (string)llCeil(llFrand(50.0)));
}
}

_____________________
http://ordinalmalaprop.com/forum/ - visit Ordinal's Scripting Colloquium for scripting discussion with actual working BBCode!

http://ordinalmalaprop.com/engine/ - An Engine Fit For My Proceeding, my Aethernet Journal

http://www.flickr.com/groups/slgriefbuild/ - Second Life Griefbuild Digest, pictures of horrible ad griefing and land spam, and the naming of names
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
05-11-2008 16:31
You might want to use '1+llFloor(llFrand(N))' rather than llCeil(). Most of the time it won't make a difference, but the range is technically closed at zero and open at N, so it's possible (though quite unlikely) the llCeil() will return zero.
Django Yifu
Beat Island Gaffer
Join date: 7 May 2007
Posts: 189
05-12-2008 03:17
Well it's not that low. Surely it's a 1:51 chance. That seems high enough to worry about it to me.
_____________________
Tread softly upon the Earth for you walk on my face.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
05-12-2008 08:12
From: Django Yifu
Well it's not that low. Surely it's a 1:51 chance. That seems high enough to worry about it to me.

No, it's not THAT bad. It has more to do with the internals of the (pseudo-)random number generator. If the random number is 0.00000001 it will still round to 1.0, but zero is technically included. If the main bit of the internal algorithm generates integers between 0 and 32767, for example, then the chance of getting a zero would only be 1/32767.
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
05-12-2008 08:15
From: Django Yifu
Well it's not that low. Surely it's a 1:51 chance. That seems high enough to worry about it to me.

No. You're basing this on the final value after the conversion to integer. But Frand(50.0) doesn't return results from a space of 50 or 51 possibilities, it returns from the space of all floating point values in [0.0, 50.0), ignoring the limits of the algorithm. So there's actually a huge number of possible results, only one of which - 0.0 - will trigger the problem.

But programming is about getting the details right, not just usually right. So the 1 + llFloor approach is the one to use. I'm sure we can all think of bugs in SL that got out the door because something was rare enough that the developers didn't detected it earlier.
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
05-12-2008 08:36
Casting to integer should be faster then the simulator processing llFloor:

(string)(integer)(llFrand(50.0)+ 1)
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
05-12-2008 08:48
From: Jesse Barnett
Casting to integer should be faster then the simulator processing llFloor.

True. I actually do it that way when I am coding, so I'm not sure why I suggested the other. Probably the initial use of math functions and a higher intuitive tie between them and well-specified, precise behavior.
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
05-12-2008 08:53
From: Hewee Zetkin
True. I actually do it that way when I am coding, so I'm not sure why I suggested the other. Probably the initial use of math functions and a higher intuitive tie between them and well-specified, precise behavior.

All for the good anyways. Threads like this are real gems for the new scripters, furthering thier understanding of the different functions.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
HeZkeZl Slade
Registered User
Join date: 5 May 2007
Posts: 20
05-12-2008 13:09
[edit] made an invalid assumption.. :) Deleted message to avoid looking like an idiot :P
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
05-12-2008 13:58
From: Kidd Krasner
No. You're basing this on the final value after the conversion to integer. But Frand(50.0) doesn't return results from a space of 50 or 51 possibilities, it returns from the space of all floating point values in [0.0, 50.0), ignoring the limits of the algorithm. So there's actually a huge number of possible results, only one of which - 0.0 - will trigger the problem.

But programming is about getting the details right, not just usually right. So the 1 + llFloor approach is the one to use. I'm sure we can all think of bugs in SL that got out the door because something was rare enough that the developers didn't detected it earlier.

No, you are quite right. Using 1 + llFloor instead of llCeil here is the appropriate thing to do. The fact that the possibility of it returning an inappropriate result is miniscule (I have never encountered it, and I have generated an awful lot of random numbers) is not the point; working from the logic is not only less likely to return something inappropriate but also more elegant and ethically sound.
_____________________
http://ordinalmalaprop.com/forum/ - visit Ordinal's Scripting Colloquium for scripting discussion with actual working BBCode!

http://ordinalmalaprop.com/engine/ - An Engine Fit For My Proceeding, my Aethernet Journal

http://www.flickr.com/groups/slgriefbuild/ - Second Life Griefbuild Digest, pictures of horrible ad griefing and land spam, and the naming of names
Skell Dagger
Smitten
Join date: 26 Jun 2007
Posts: 1,885
05-14-2008 12:57
Thanks for all the replies! I'll try that out this weekend. Out of the two suggestions, which would be better: llFloor or casting to integer? (Says he, making out like he knows what the HELL he's saying...)

Also, yeah, I knew someone would call me on dice/die *g* I blame the lateness of the hour of my posting...
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
05-14-2008 13:55
From: Skell Dagger
Out of the two suggestions, which would be better: llFloor or casting to integer?

It doesn't really matter. Casting to an integer is probably slightly faster. llFloor() is more explicit and is thus possibly a little bit more readable/maintainable. In this case they should always have the same semantics. For negative numbers they would be different as casting truncates (rounds toward zero).