Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

returning random strings from a list

Aniam Ingmann
llOwnage(float pwnage);
Join date: 22 Oct 2005
Posts: 206
10-26-2007 19:45
How would I make a list of strings and have the script choose one at random?
_____________________
From: someone

Don't worry, Aniam is here!
- Noob
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
10-26-2007 20:17
list data =[
"something",
"something else",
"stuff",
"bob",
"jay z"];

default
{
state_entery()
{
llOwnerSay(llList2String(data,llFloor(llFrand(4))));
}
}

or something like that
Aniam Ingmann
llOwnage(float pwnage);
Join date: 22 Oct 2005
Posts: 206
10-26-2007 20:34
kk thanks
_____________________
From: someone

Don't worry, Aniam is here!
- Noob
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
10-26-2007 20:44
Here is an interesting thread you make want to look at:

/54/05/207690/1.html
_____________________
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
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
10-26-2007 21:34
From: Osgeld Barmy
list data =[
"something",
"something else",
"stuff",
"bob",
"jay z"];

default
{
state_entery()
{
llOwnerSay(llList2String(data,llFloor(llFrand(4))));
}
}

or something like that

Why use llFloor instead of typecasting to an integer? Also, doesn't llFrand return values [0, input)? So by putting in 4, you'll never retrieve the last list element.
_____________________
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
10-26-2007 23:12
From: Tyken Hightower
Why use llFloor instead of typecasting to an integer?


cause i wrote it while not inworld, off the top of my head in like 3 seconds

From: Tyken Hightower

Also, doesn't llFrand return values [0, input)? So by putting in 4, you'll never retrieve the last list element.


llFrand does, theres 5 elements 0-4, but im sure it doesnt work like that, that would be too easy ...

edit it doesnt do 0-4 it does (while x < 4) which is a silly quirk i forgot about, so yea it needs to be 5

also see above

that script was a quick nudge in the right direction, nothing more
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
10-27-2007 07:38
CODE

list data = ["Aniam", "Osgeld", "Tyken", "Jesse"];

default
{
touch_start(integer total_number)
{
integer list_length = llGetListLength(data);
integer random = (integer)llFrand(list_length);
llSay(0, llList2String(data, random) + " is the Ruler of the forum!");
}
}

OMG! It's ME!!!!!!! I am the Ruler of the forum!!!!!
Shucks! Now Osgeld is.......................................
_____________________
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
Aniam Ingmann
llOwnage(float pwnage);
Join date: 22 Oct 2005
Posts: 206
10-27-2007 08:07
Haha, thanks jesse. You're always helpin' me out.
_____________________
From: someone

Don't worry, Aniam is here!
- Noob
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
10-27-2007 09:25
From: Jesse Barnett
Shucks! Now Osgeld is.......................................


out of practice, cause osgeld has been writing lua and php (not connected) for the last 5 months and not lsl ;)

so jesse you can have the title of king, for now, but i shall return!
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
10-28-2007 07:59
is that better than

list someList =["1", "2", "3", "4", "5", "6"];

llSay( 0, "die rolled: " + llList2String( llListRandomize( someList, 1), 0 ) );
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
10-28-2007 08:32
From: Void Singer
is that better than

list someList =["1", "2", "3", "4", "5", "6"];

llSay( 0, "die rolled: " + llList2String( llListRandomize( someList, 1), 0 ) );


Good question.

Let's compare what they have to do:

CODE

list data = ["Aniam", "Osgeld", "Tyken", "Jesse"];
integer list_length;

default
{
state_entry()
{
list_length = llGetListLength(data);//No use in making it check list length everytime
}
touch_start(integer total_number)
{
integer random = (integer)llFrand(list_length);
llSay(0, llList2String(data, random) + " is the Ruler of the forum!");
}
}

Makes the simulator do this with every touch:

"*** touch_start(1)
Frand(4.00000)=1.85173216920892
List2String([Aniam,Osgeld,Tyken,Jesse],1)=Osgeld"

CODE

list data = ["Aniam", "Osgeld", "Tyken", "Jesse"];

default
{
touch_start(integer total_number)
{
data = llListRandomize(data,1);
llSay(0, llList2String(data, 0) + " is the Ruler of the forum!");
}
}

The simulator does this:
*** touch_start(1)
ListRandomize([Aniam,Osgeld,Tyken,Jesse],1)=[Jesse,Osgeld,Aniam,Tyken]
List2String([Jesse,Osgeld,Aniam,Tyken],0)=Jesse"

So both do a good job. And with a short list probably equal But as the size of the list grows............Option one would be better because you are not having to manipulate the list itslf, which can be expensive when it comes to long lists.

A more clear example would be the link I posted above and compare what has to happen to get a random output:

CODE

list quotelist =[
"To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).",
"Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry. - (Hamlet, Act I, Scene III).",
"This above all: to thine own self be true. - (Hamlet, Act I, Scene III).",
"Though this be madness, yet there is method in 't.. - (Hamlet, Act II, Scene II).",
"That it should come to this!. - (Hamlet, Act I, Scene II).",
"There is nothing either good or bad, but thinking makes it so. - (Hamlet, Act II, Scene II).",
"What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! . - (Hamlet, Act II, Scene II).",
"The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II).",
"In my mind's eye. - (Hamlet, Act I, Scene II).",
"A little more than kin, and less than kind. - (Hamlet, Act I, Scene II).",
"The play 's the thing wherein I'll catch the conscience of the king. - (Hamlet, Act II, cene II).",
"And it must follow, as the night the day, thou canst not then be false to any man. - (Hamlet, Act I, Scene III).",
"This is the very ecstasy of love. - (Hamlet, Act II, Scene I).",
"Brevity is the soul of wit. - (Hamlet, Act II, Scene II).",
"Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love. - (Hamlet, Act II, Scene II).",
"Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I).",
"Do you think I am easier to be played on than a pipe? - (Hamlet, Act III, Scene II).",
"I will speak daggers to her, but use none. - (Hamlet, Act III, Scene II).",
"When sorrows come, they come not single spies, but in battalions. - (Hamlet, Act IV, Scene V).",
"All the world 's a stage, and all the men and women merely players. They have their exits and their entrances - And one man in his time plays many parts - (As You Like It, Act II, Scene VII).",
"Can one desire too much of a good thing?. - (As You Like It, Act IV, Scene I).",
"I like this place and willingly could waste my time in it - (As You Like It, Act II, Scene IV).",
"How bitter a thing it is to look into happiness through another man's eyes! - (As You Like It, Act V, Scene II).",
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude. - (As You Like It, Act II, Scene VII).",
"True is it that we have seen better days. - (As You Like It, Act II, Scene VII). "
];

default
{
touch_start(integer total_number)
{
integer quote = (integer)llFrand(24.0);
llSay(0, llList2String(quotelist, quote));
}
}


The simulator does this:
"*** touch_start(1)
Frand(24.00000)=15.7999644204043
List2String([To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).,Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry. - (Hamlet, Act I, Scene III).,This above all: to thine own self be true. - (Hamlet, Act I, Scene III).,Though this be madness, yet there is method in 't.. - (Hamlet, Act II, Scene II).,That it should come to this!. - (Hamlet, Act I, Scene II).,There is nothing either good or bad, but thinking makes it so. - (Hamlet, Act II, Scene II).,What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! . - (Hamlet, Act II, Scene II).,The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II).,In my mind's eye. - (Hamlet, Act I, Scene II).,A little more than kin, and less than kind. - (Hamlet, Act I, Scene II).,The play 's the thing wherein I'll catch the conscience of the king. - (Hamlet, Act II, cene II).,And it must follow, as the night the day, thou canst not then be false to any man. - (Hamlet, Act I, Scene III).,This is the very ecstasy of love. - (Hamlet, Act II, Scene I).,Brevity is the soul of wit. - (Hamlet, Act II, Scene II).,Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love. - (Hamlet, Act II, Scene II).,Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I).,Do you think I am easier to be played on than a pipe? - (Hamlet, Act III, Scene II).,I will speak daggers to her, but use none. - (Hamlet, Act III, Scene II).,When sorrows come, they come not single spies, but in battalions. - (Hamlet, Act IV, Scene V).,All the world 's a stage, and all the men and women merely players. They have their exits and their entrances - And one man in his time plays many parts - (As You Like It, Act II, Scene VII).,Can one desire too much of a good thing?. - (As You Like It, Act IV, Scene I).,I like this place and willingly could waste my time in it - (As You Like It, Act II, Scene IV).,How bitter a thing it is to look into happiness through another man's eyes! - (As You Like It, Act V, Scene II).,Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude. - (As You Like It, Act II, Scene VII).,True is it that we have seen better days. - (As You Like It, Act II, Scene VII). ],15)=Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I)."

CODE

list quotelist =[
"To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).",
"Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry. - (Hamlet, Act I, Scene III).",
"This above all: to thine own self be true. - (Hamlet, Act I, Scene III).",
"Though this be madness, yet there is method in 't.. - (Hamlet, Act II, Scene II).",
"That it should come to this!. - (Hamlet, Act I, Scene II).",
"There is nothing either good or bad, but thinking makes it so. - (Hamlet, Act II, Scene II).",
"What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! . - (Hamlet, Act II, Scene II).",
"The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II).",
"In my mind's eye. - (Hamlet, Act I, Scene II).",
"A little more than kin, and less than kind. - (Hamlet, Act I, Scene II).",
"The play 's the thing wherein I'll catch the conscience of the king. - (Hamlet, Act II, cene II).",
"And it must follow, as the night the day, thou canst not then be false to any man. - (Hamlet, Act I, Scene III).",
"This is the very ecstasy of love. - (Hamlet, Act II, Scene I).",
"Brevity is the soul of wit. - (Hamlet, Act II, Scene II).",
"Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love. - (Hamlet, Act II, Scene II).",
"Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I).",
"Do you think I am easier to be played on than a pipe? - (Hamlet, Act III, Scene II).",
"I will speak daggers to her, but use none. - (Hamlet, Act III, Scene II).",
"When sorrows come, they come not single spies, but in battalions. - (Hamlet, Act IV, Scene V).",
"All the world 's a stage, and all the men and women merely players. They have their exits and their entrances - And one man in his time plays many parts - (As You Like It, Act II, Scene VII).",
"Can one desire too much of a good thing?. - (As You Like It, Act IV, Scene I).",
"I like this place and willingly could waste my time in it - (As You Like It, Act II, Scene IV).",
"How bitter a thing it is to look into happiness through another man's eyes! - (As You Like It, Act V, Scene II).",
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude. - (As You Like It, Act II, Scene VII).",
"True is it that we have seen better days. - (As You Like It, Act II, Scene VII). "
];

default
{
touch_start(integer total_number)
{
quotelist = llListRandomize(quotelist,1);
llSay(0, llList2String(quotelist, 0));
}
}

The simulator has to do ALL of this:
"*** state_entry()
*** touch_start(1)
ListRandomize([To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).,Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry. - (Hamlet, Act I, Scene III).,This above all: to thine own self be true. - (Hamlet, Act I, Scene III).,Though this be madness, yet there is method in 't.. - (Hamlet, Act II, Scene II).,That it should come to this!. - (Hamlet, Act I, Scene II).,There is nothing either good or bad, but thinking makes it so. - (Hamlet, Act II, Scene II).,What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! . - (Hamlet, Act II, Scene II).,The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II).,In my mind's eye. - (Hamlet, Act I, Scene II).,A little more than kin, and less than kind. - (Hamlet, Act I, Scene II).,The play 's the thing wherein I'll catch the conscience of the king. - (Hamlet, Act II, cene II).,And it must follow, as the night the day, thou canst not then be false to any man. - (Hamlet, Act I, Scene III).,This is the very ecstasy of love. - (Hamlet, Act II, Scene I).,Brevity is the soul of wit. - (Hamlet, Act II, Scene II).,Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love. - (Hamlet, Act II, Scene II).,Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I).,Do you think I am easier to be played on than a pipe? - (Hamlet, Act III, Scene II).,I will speak daggers to her, but use none. - (Hamlet, Act III, Scene II).,When sorrows come, they come not single spies, but in battalions. - (Hamlet, Act IV, Scene V).,All the world 's a stage, and all the men and women merely players. They have their exits and their entrances - And one man in his time plays many parts - (As You Like It, Act II, Scene VII).,Can one desire too much of a good thing?. - (As You Like It, Act IV, Scene I).,I like this place and willingly could waste my time in it - (As You Like It, Act II, Scene IV).,How bitter a thing it is to look into happiness through another man's eyes! - (As You Like It, Act V, Scene II).,Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude. - (As You Like It, Act II, Scene VII).,True is it that we have seen better days. - (As You Like It, Act II, Scene VII). ],1)=[The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II).,This is the very ecstasy of love. - (Hamlet, Act II, Scene I).,And it must follow, as the night the day, thou canst not then be false to any man. - (Hamlet, Act I, Scene III).,Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I).,Do you think I am easier to be played on than a pipe? - (Hamlet, Act III, Scene II).,Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude. - (As You Like It, Act II, Scene VII).,Though this be madness, yet there is method in 't.. - (Hamlet, Act II, Scene II).,Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry. - (Hamlet, Act I, Scene III).,True is it that we have seen better days. - (As You Like It, Act II, Scene VII). ,I will speak daggers to her, but use none. - (Hamlet, Act III, Scene II).,To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).,In my mind's eye. - (Hamlet, Act I, Scene II).,The play 's the thing wherein I'll catch the conscience of the king. - (Hamlet, Act II, cene II).,All the world 's a stage, and all the men and women merely players. They have their exits and their entrances - And one man in his time plays many parts - (As You Like It, Act II, Scene VII).,I like this place and willingly could waste my time in it - (As You Like It, Act II, Scene IV).,There is nothing either good or bad, but thinking makes it so. - (Hamlet, Act II, Scene II).,Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love. - (Hamlet, Act II, Scene II).,When sorrows come, they come not single spies, but in battalions. - (Hamlet, Act IV, Scene V).,This above all: to thine own self be true. - (Hamlet, Act I, Scene III).,How bitter a thing it is to look into happiness through another man's eyes! - (As You Like It, Act V, Scene II).,Can one desire too much of a good thing?. - (As You Like It, Act IV, Scene I).,What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! . - (Hamlet, Act II, Scene II).,A little more than kin, and less than kind. - (Hamlet, Act I, Scene II).,That it should come to this!. - (Hamlet, Act I, Scene II).,Brevity is the soul of wit. - (Hamlet, Act II, Scene II).]
List2String([The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II).,This is the very ecstasy of love. - (Hamlet, Act II, Scene I).,And it must follow, as the night the day, thou canst not then be false to any man. - (Hamlet, Act I, Scene III).,Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I).,Do you think I am easier to be played on than a pipe? - (Hamlet, Act III, Scene II).,Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude. - (As You Like It, Act II, Scene VII).,Though this be madness, yet there is method in 't.. - (Hamlet, Act II, Scene II).,Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry. - (Hamlet, Act I, Scene III).,True is it that we have seen better days. - (As You Like It, Act II, Scene VII). ,I will speak daggers to her, but use none. - (Hamlet, Act III, Scene II).,To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).,In my mind's eye. - (Hamlet, Act I, Scene II).,The play 's the thing wherein I'll catch the conscience of the king. - (Hamlet, Act II, cene II).,All the world 's a stage, and all the men and women merely players. They have their exits and their entrances - And one man in his time plays many parts - (As You Like It, Act II, Scene VII).,I like this place and willingly could waste my time in it - (As You Like It, Act II, Scene IV).,There is nothing either good or bad, but thinking makes it so. - (Hamlet, Act II, Scene II).,Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love. - (Hamlet, Act II, Scene II).,When sorrows come, they come not single spies, but in battalions. - (Hamlet, Act IV, Scene V).,This above all: to thine own self be true. - (Hamlet, Act I, Scene III).,How bitter a thing it is to look into happiness through another man's eyes! - (As You Like It, Act V, Scene II).,Can one desire too much of a good thing?. - (As You Like It, Act IV, Scene I).,What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! . - (Hamlet, Act II, Scene II).,A little more than kin, and less than kind. - (Hamlet, Act I, Scene II).,That it should come to this!. - (Hamlet, Act I, Scene II).,Brevity is the soul of wit. - (Hamlet, Act II, Scene II).],0)=The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II)."
_____________________
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
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
10-28-2007 08:43
And this, my friends is why the more advanced scripters who have poo poo'd LSLEditor in the past really need to take a second look at it. You can see what is actually happening as the script runs. Plus you can also now use lslint as a plugin with it.
_____________________
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