Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Need help with generating random string script

MayaButterfly Sands
Registered User
Join date: 13 Feb 2007
Posts: 11
02-28-2007 06:56
I'm trying to make an object say a random phrase on touch. I know how to make an object speak on touch. I think I need to (at the beginning) say for example:

list STRINGS=["Have a great day!","You are amazing!","Go for it!"];

How do I use llFRAND in conjuction with this? Actually, how do I use llFRAND?
Anti Antonelli
Deranged Toymaker
Join date: 25 Apr 2006
Posts: 1,091
02-28-2007 07:44
SL is down for maintenance so I can't test and there is likely to be a dumb error in here, but this should work to illustrate a couple of concepts anyway. Comments below since they are kind of long and screw up the php formatting ;)

CODE

list STRINGS=["Have a great day!","You are amazing!","Go for it!"];
integer number_of_phrases;

default
{
state_entry()
{
number_of_phrases = llGetListLength(STRINGS);
}

touch_start(integer num_detected)
{
integer random_integer = llFloor(llFrand(number_of_phrases));

llSay(0, llList2String(STRINGS,random_integer));
}
}


A few things wrapped up together in there...

llGetListLength returns the number of elements in the list; in this example, 3.

llFrand then returns a float between 0 and 2.9999. Using llFloor on that is a trick from the wiki; what it does is round everything *down*. Doing that happens to give you a perfect distribution of random integers. As a side benefit here, we need integers starting from 0 for the next bit, so it works out rather well.

llList2String turns a list element at a certain position (index) into a string. Since the index numbers start at 0, we can just plug in the random integer from above.

Hope that helps some :)


edit: caught the above-mentioned dumb error, hopefully that was the only one :D
edit2: nope, thanks Deanna :p
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
02-28-2007 10:19
From: Anti Antonelli
Using llFloor on that is a trick from the wiki; what it does is round everything *down*.
I just typecast to integer, same result.


From: someone
edit: caught the above-mentioned dumb error, hopefully that was the only one :D
You missed the closing parenth for llSay after llList2String. :p
MayaButterfly Sands
Registered User
Join date: 13 Feb 2007
Posts: 11
02-28-2007 10:25
Thank you both for your help. It wasn't compiling at first, but I have put the missing ) in the wrong spot.
Winter Ventura
Eclectic Randomness
Join date: 18 Jul 2006
Posts: 2,579
02-28-2007 10:48
Try this

CODE

list STRINGS=["Have a great day!","You are amazing!","Go for it!"];
integer number_of_phrases;

default
{
state_entry()
{
number_of_phrases = llGetListLength(STRINGS);
}

touch_start(integer num_detected)
{
integer random_integer = llFloor(llFrand((float)number_of_phrases));

llSay(0, llList2String(STRINGS,random_integer);
}
}
_____________________

● Inworld Store: http://slurl.eclectic-randomness.com
● Website: http://www.eclectic-randomness.com
● Twitter: @WinterVentura