Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

llFrand starts with same values how to "reset"?

Rambladoes Ruml
Registered User
Join date: 2 Aug 2006
Posts: 1
09-02-2006 14:33
I have made a "dropper" that rezzes physical balls (they die after 45 secs).

I want the balls to have random colors so i put the script below in them to randomly change the color with the llFrand function. Problem is that with every new object ( the ball dropped) llFrand starts with the same "random" number. How can i "reset" llFrand so it generates a different value?



default
{
state_entry()
{

// choose three random RGB color components between 0. and 1.0.
float redness = llFrand( 1.0 );
float greenness = llFrand( 1.0 );
float blueness = llFrand( 1.0 );

// combine color components into a vector and use that vector
// to set object color.
vector prim_color = < redness, greenness, blueness >;
llSetColor( prim_color, ALL_SIDES ); // set object color to new color.



llSleep(45.0);
llDie();
}
}
Jillian Callahan
Rotary-winged Neko Girl
Join date: 24 Jun 2004
Posts: 3,766
09-02-2006 15:03
CODE
default
{
state_entry() // Triggers only on state_entry. Further rezzing will *not* run this code.
{
// choose three random RGB color components between 0. and 1.0.
float redness = llFrand( 1.0 );
float greenness = llFrand( 1.0 );
float blueness = llFrand( 1.0 );

// combine color components into a vector and use that vector
// to set object color.
vector prim_color = < redness, greenness, blueness >;
llSetColor( prim_color, ALL_SIDES ); // set object color to new color.



llSleep(45.0);
llDie();
}

on_rez() // but, reset the script on rez, and it re-enters the state!
{
llResetScript();
}
}
_____________________
Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
09-02-2006 18:15
You can also use something else that will never be the same to further randomize the randomize function, like multiplying by the current time and normalizing the result.

Baron H.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
09-03-2006 07:54
Thats not actualy valid Baron...
What you suggest would either unevenly weight the output, or be removed by the normalization.

Jillian has the right answer. Scripts don't reset on rez by them selves.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
09-03-2006 13:01
I was actually thinking of using a time and mod() function. One would strip off days, hours, minutes, and be left with seconds; divide that by 60 and multiply by a random number. I don't think that would unevenly distribute nor undo the added effect.

I am assuming that Rambladoes is creating something so that new prims (and scripts) are created . . . won't every new object that is created use the state_entry() event? Maybe I'm misunderstanding something here . . .

Baron H.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
09-03-2006 21:53
From: Baron Hauptmann
I was actually thinking of using a time and mod() function. One would strip off days, hours, minutes, and be left with seconds; divide that by 60 and multiply by a random number. I don't think that would unevenly distribute nor undo the added effect.

I am assuming that Rambladoes is creating something so that new prims (and scripts) are created . . . won't every new object that is created use the state_entry() event? Maybe I'm misunderstanding something here . . .

Baron H.


The distribution you're describing is not even. So you start with the float based on the seconds, 0->0.983333333; then multiply by a mystery random, 0->1. So lets work this backwords. Say the result of the multiplication is 0.98; what could the inputs have been? 59 seconds & 0.99661016949; nothing else. Then lets look at what could result in 0.5: 30 seconds and 1, 42 seconds & 0.71428571 (and there are 29 in total that satisfy 0.5). The distribution is uneven. QED.

state_entry() is only triggered when the script is compiled or when the state is changed. The script state is saved when you take the object, so when the script is rezed the state_entry has already been executed.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Adrian Zobel
Registered User
Join date: 4 Jan 2006
Posts: 49
09-04-2006 08:40
Can you have the dropper pass a random integer in the paramater and have the ball use llGetStartParameter?
Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
09-04-2006 08:50
Okay. I see what you mean.

And I played around a little myself just to prove you were right on the state_entry issue. I had really thought that if a script did a llRezObject, since the object was a copy of the one stored in the original prim, that it would start with the state_entry. I knew I should have deferred to your experience, but, well, . . . . no I SEE that you were right :)

An initial conclusion I draw from this . . . it seems it is impossible to rez an object via a script that has its script already reset. I cannot have it ready to run *and* reset before putting it in inventory or in another prim. So I need to make sure that I either reset it via the rezzing script or use a on_rez event.

Baron H.
Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
09-04-2006 08:52
Adrian:

He was wanting to have 3 independently random numbers to control color. I'm not sure he could do that as a start param. He could send it another way, by llSaying a random vector or something, though.

Baron H.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
09-04-2006 09:52
Since SL's color is basicly just RGB, you can pack them into an integer.

CODE

integer c2i(vector c)
{
c*=255;
return ((integer)c.x & 255) | (((integer)c.y & 255) << 8) | (((integer)c.z & 255) << 16);
}

vector i2c(integer i)
{
return <(c & 255), ((c >> 8) & 255), ((c >> 16) & 255)> / 255;
}


You could use llGetStartParameter or the param from on_rez (same thing).

You could keep the state_entry from running when you save the script by doing so on no-script land.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
grumble Loudon
A Little bit a lion
Join date: 30 Nov 2005
Posts: 612
09-04-2006 13:26
If I wanted random numbers I would grab some unguesable item and use it to grab n random numbers and discard them.

In terms of randomness, the more random environment things the better.
Some posibilites: time, sim FPS, User interaction, script time,ect..

For example start a timer at some non integer interval and grab the last digit llGetTimeOfDay().

Most gambling machines constantly grab random numbers using a timer to keep it spinning and the actual number used is based on what the number is whe person starts the machine.

rember that you can combine mutiple random numbers by casting them to integers in a fixed range and then use Xor to combine them.

Edit:
CODE

//Start{
// start timer(1.4)
//}
//on timer{
// get a random number
//}
//on_rez{
// Change the prim color based on more random numbers
//}


Edit: I updated the wiki
http://secondlife.com/badgeo/wakka.php?wakka=llFrand
Champie Jack
Registered User
Join date: 6 Dec 2003
Posts: 1,156
09-04-2006 16:43
From: grumble Loudon


Will somebody please explain why the WIKI needed updating. Perhaps I am misunderstanding something in this thread, but grumble Loudon's assertion that "Note: as of 1.10 the result from llFrand() repeats the same sequence after a script reset." is not accurate (from the wiki revision).

use on_rez to reset the script by calling a user-defined function or changing to a state that initailizes the script.

If I have missed some obscure point or reason for grumble's statements, then please disregard my comments.

Champie