Jump to content
Sign in to follow this  
thesammy58

Random Timer

You are about to reply to a thread that has been inactive for 2273 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Isn't that what you expected?  The most it could be is 10 seconds, and the least it could be is 10 - 1 = 9 seconds.  Then, if you have cast your result as an integer, it will always be 9 seconds.

Share this post


Link to post
Share on other sites

That wasn't what I was expecting. :matte-motes-stress: Should I change the numbers to 10 and 9 so it will choose between 10 through 1?

That doesn't seem to work either.

Share this post


Link to post
Share on other sites

Two ways to find out:

1. Do the math.

2. Experiment.

Or (different approach):

llSetTimerEvent(1.0 + llFrand(9.0));

Share this post


Link to post
Share on other sites

This is very irritating... I copied in what you wrote and now it still doesn't seem to be choosing a random time. I can predict in my head when the timer is going to go off again, and that's not what I want. I'm doing the math and everything but it just doesn't seem to be choosing a random number.

 

I have the timer going off over and over again, like in a loop. Should I reset the script each time it goes off or something?

Share this post


Link to post
Share on other sites

llFrand(X) produces a random number between X and zero if X is negative, and between 0 and X is X is positive. You don't want a negative timer value (though it would be super cool if we could go back in time!),  so X must always be positive. If you want a random time that is one second at the minimum and 10 seconds at the maximum (lets call those two values "min" and "max"), then llFrand(X) alone won't do the job, as it alway has the potential to produce... zero. 

So, you must add something to the llFrand(X) result to keep it at or above your "min" value. And, since llFrand(X) can go all the way to zero, the value you must add actually is your min value. So you've now got something of the form...

min + llFrand(X)

You're not done yet though. What do you use as the value of X if you want the result of that statement to have your maximum value of 10? You can't use 10 as "max", as you've now added "min" to it. So you must decrease max by exactly the value of min, producing...

min + llFrand(max-min)

And there's the value you'll use in your llSetTimerEvent() call.

So, to make your script easy to understand and edit, declare two global variables (min and max) at the beginning of the script and set their values as you wish. Then, rather than trying to do the math every time you want to change the times, you just plug in the minimum and maximum times you want and let the LSL compiler do the heavy lifting.

Remember, as in the LSL example page, you must call llSetTimerEvent both initially, to get the timer going, and within the timer event itself, to keep it going. Should you wish to stop the timer, you must have llSetTimerEvent(0) in whatever event handler (touch_start() in the example) you want to cause the stoppage.

Yes, we're making you do some thinking here. We want you to join us in the ranks of people who can raise Holy Hell by writing scripts.

Good luck, Sammy!

Share this post


Link to post
Share on other sites
default{     state_entry()     {          llResetTime();    // This is just to set a marker for the start time, to demonstrate          llSetTimerEvent(1.0 + llFrand(9.0));     }     timer()     {          llSay(0, "This timer was triggered " + (string)llGetAndResetTime() + " seconds ago.");     }}

 

Share this post


Link to post
Share on other sites

Oooh,

Now I'm wondering whether Sammy wants a timer that regularly paces off an initial random time interval, or one that fires at random intervals.

Your example does the former, the LSL example I linked does the latter.

Share this post


Link to post
Share on other sites

In that case....

default{     state_entry()     {          llResetTime();    // This is just to set a marker for the start time, to demonstrate          llSetTimerEvent(1.0 + llFrand(9.0));     }     timer()     {          llSay(0, "This timer was triggered " + (string)llGetAndResetTime() + " seconds ago.");          llSetTimerEvent(1.0 + llFrand(9.0));   // a NEW timer interval.        }}

 

Share this post


Link to post
Share on other sites

llResetScript is a clumsy way out.  You should never have to reset a script just to change the clock.  It might have helped if we knew which of Maddy's two possible cases you were trying for.  I gave you sample scripts for both now.  Pick one.  :smileywink:

Share this post


Link to post
Share on other sites

llResetScript takes much more server time than llSetTimerEvent, and it makes the script very difficult to expand in the future. Should you ever want the script to remember anything, calling llResetScript is akin to knocking it unconscious and stealing it's recollection.

Share this post


Link to post
Share on other sites

Put another way, llResetScript() lacks finesse.  Why would you want to stop an entire script that may be doing dozens of things -- listening, moving things around, turning lights onand off --- just because you want to change the clock?  As Maddy says, resetting the script will make it forget everything that it was doing, which is a good practical reason for not doing it.  As far as I'm concerned, it's just inelegant, like using a shotgun to kill a fly.

Share this post


Link to post
Share on other sites


Rolig Loon wrote:

As far as I'm concerned, it's just inelegant, like using a shotgun to kill a fly.

Geez Rolig, that's an awesome way to kill a fly. Couldn't you find a better analogy, like using using a jack hammer to drive a scr... no wait... wearing a padded bra to attract a... never mind.

...goes looking for a fly to kill.

Share this post


Link to post
Share on other sites

To my mind, llResetScript() is one of the two functions beginner scripters should be very careful about using (the other, for rather different reasons, is llSleep()).

The trouble with llResetScript() is that it can, indeed, fix a multitude of problems.   However, because it's not fixing whatever the underlying cause of the problem is, it can make the the script very difficult to adapt in future.  Better, by far, to get to the root of the problem and fix that, so you don't need to reset everything, which often you will find you really don't want to anyway.

Save llResetScript for circumstances where you're sure you want to wipe everything (a change of owner, for example).

Share this post


Link to post
Share on other sites
You are about to reply to a thread that has been inactive for 2273 days.

Please take a moment to consider if this thread is worth bumping.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...