thesammy58 Posted April 13, 2014 Share Posted April 13, 2014 How can I set a timer to choose a random time between two integers? Link to comment Share on other sites More sharing options...
Madelaine McMasters Posted April 13, 2014 Share Posted April 13, 2014 Hi Sammy, There's an example of what you're looking for in the LSL Wiki, in the middle of this page. Link to comment Share on other sites More sharing options...
thesammy58 Posted April 13, 2014 Author Share Posted April 13, 2014 Would I do this? llSetTimerEvent( 30.0 - llFrand(15.0) ); And then replace those numbers? Link to comment Share on other sites More sharing options...
Rolig Loon Posted April 13, 2014 Share Posted April 13, 2014 That will certainly work, if you want a timer interval between 15 and 30 seconds. Link to comment Share on other sites More sharing options...
thesammy58 Posted April 13, 2014 Author Share Posted April 13, 2014 I changed the numbers to 10 and 1 and all it did was set the timer off every 9 seconds? Link to comment Share on other sites More sharing options...
Rolig Loon Posted April 13, 2014 Share Posted April 13, 2014 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. Link to comment Share on other sites More sharing options...
thesammy58 Posted April 13, 2014 Author Share Posted April 13, 2014 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. Link to comment Share on other sites More sharing options...
Rolig Loon Posted April 13, 2014 Share Posted April 13, 2014 Two ways to find out: 1. Do the math. 2. Experiment. Or (different approach): llSetTimerEvent(1.0 + llFrand(9.0)); Link to comment Share on other sites More sharing options...
thesammy58 Posted April 13, 2014 Author Share Posted April 13, 2014 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? Link to comment Share on other sites More sharing options...
Madelaine McMasters Posted April 13, 2014 Share Posted April 13, 2014 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! 1 Link to comment Share on other sites More sharing options...
Rolig Loon Posted April 13, 2014 Share Posted April 13, 2014 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."); }} Link to comment Share on other sites More sharing options...
Madelaine McMasters Posted April 13, 2014 Share Posted April 13, 2014 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. Link to comment Share on other sites More sharing options...
thesammy58 Posted April 13, 2014 Author Share Posted April 13, 2014 Thanks to both of you. :matte-motes-smile: I put in an llResetScript(); under the timer event and that seems to have fixed it. Link to comment Share on other sites More sharing options...
Rolig Loon Posted April 13, 2014 Share Posted April 13, 2014 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. }} Link to comment Share on other sites More sharing options...
Rolig Loon Posted April 13, 2014 Share Posted April 13, 2014 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: Link to comment Share on other sites More sharing options...
thesammy58 Posted April 13, 2014 Author Share Posted April 13, 2014 Does llResetScript tend to cause region lag or anything like that? Link to comment Share on other sites More sharing options...
Madelaine McMasters Posted April 13, 2014 Share Posted April 13, 2014 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. Link to comment Share on other sites More sharing options...
Rolig Loon Posted April 13, 2014 Share Posted April 13, 2014 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. Link to comment Share on other sites More sharing options...
Madelaine McMasters Posted April 13, 2014 Share Posted April 13, 2014 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. Link to comment Share on other sites More sharing options...
Innula Zenovka Posted April 13, 2014 Share Posted April 13, 2014 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). Link to comment Share on other sites More sharing options...
thesammy58 Posted April 13, 2014 Author Share Posted April 13, 2014 Thank you everyone for your input. :matte-motes-big-grin: I will be sure to change llResetScript now that I know the depths of it. Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now