Jump to content
You are about to reply to a thread that has been inactive for 4288 days.

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

Recommended Posts

Posted

I wanted this script to run only if it daytime in SL.  But I"ve tried putting the llGetSunDirection in the timer...that didn't work, so I tried moving it to right after state_entry, and that wasn't it either.  Any help greatly appreciated. 

 

integer LINK_MOVE_ME = 352;         // tell mover to rest for str seconds

default
{
   state_entry()
  {
      
    llSetTimerEvent(60);  // check every minite
  }
    timer()
    {
        
        vector sun = llGetSunDirection();
        if (sun.z < 0)
        if (llFrand(10) < 5.0)  // 50% chance it will suddenly yodel
       {   
         llMessageLinked(LINK_SET, LINK_MOVER,   "8", "");        // tell mover to rest for 8 seconds
         llPlaySound("sing",1.0);
        llMessageLinked(LINK_SET, 1,   "eat", "");        // play the eat animation
}
 

Posted

The first problem is that you're telling it only to run if the sun is -- notionally, at least -- below the horizon.   Try if (sun.z>0.0) instead.

The second is that this method isn't terribily reliable as a way of predicting the hours of light and darkness, in that it usually stays light well after sun.z<0.0 and becomes light well before that.   Dora Gustafson has a more accurate way of doing it here, but I am not sure how easy you would find it to adapt to your needs.    The if (sun.z>0.0) might be close enough for you.

You're missing two final } at the end of the script, of course, but I assume that's a typo since it wouldn't compile as is.

Posted

We're getting somewhere, then.

The problem may just be that you're running into a long series of results that are greater than or equal to 5.0.   You could test that by having it llOwnerSay the result of llFrand(10.0) each time and see if that's what's happening.

 

Posted

if (llFrand(10) < 5.0)  // 50% chance it will suddenly yodel

is not going to work for you very well. It returns a random float of less than 10.0 so a series of results like 4.25, 4.004, 4.78,... etc. is perfectly legitimate and would happen quite often as a matter of fact. What you need is a coin toss simulation. Look for a user function at http://wiki.secondlife.com/wiki/LlFrand

Posted

I wondered about a coin toss simulation, but then I got to thinking -- no matter how decimal points there are, there's still an equal chance that the number itself is going to be less than 5.0 or more, isn't there?  

There's going to be a random distribution of the number to the right of the decimal point, after all, which is what will determine the outcome.    That is, there's a 50/50 chance it's going to be 0-4 or 5-9.  That's certainly the impression I gained when I tested it firing once a second, checking the results with llOwnerSay.

Maybe someone who know more about stats than do I might comment.

 

Posted

Innula,  most random numbers generator algorithms do not treat the integer part and the fraction separately. I don't think the LL one is any different. 

Posted

But surely Innula is right.  The range is 0-9, so whether the actual number is 4.3556 or 4.6712 is immaterial.  The point is that it's less than 5.0000 and there ought to be a random probability of 0.5 that any particular number generated by (integer)llFrand(10.0) will be < 5.  With the understanding, of course, that llFrand is not truly random, but close enough for this sort of exercise.

Posted

Yes, but what I'm saying is that, no matter how many decimal places there are, if the random number generator is random, surely the odds are even that the random number it picks is going to be greater or lesser than 5.0.

 

Posted

No.  The odds of having a sequence of numbers 2.2350 and 6.6789, which would produce the desired result are exactly the same as the ods of having a sequence 2.2350 and 2.6789, so a long sequence of 2-s is entirely possible with this approach. All what's needed there is to look for random integers instead of floats.

Posted

Very material :) You may get 20 of 4-s in a row, all with different fractions; they are all less than 5.0 so in a simulator like this a coin would flip the same 20 times in a row. That is not what should be happening.

Posted

If we're generating integers from 0 - 9, the odds are 1 in 10 that 4 is going to come up on any particular occasion.  So the odds are 1 in  10 x 10 x 10, or 1 in 1000, that it will generate 4 three times in a row.

If, instead of generating integers, we generate floats to 6 decimal places, what do you say the odds are of generating 4.something three times in a row?

Posted

The desired result is a 50/50 %.......so there is no issue with the the line of code as it is written. the issue is just having the code run with the 50/50 chance only if it is daytime.  I appreciate the feedback and replies, but we're getting way off course of the orignial post.

Posted

Try with something like this.   

 

integer toggle;default{    state_entry()    {        //llSay(0, "Hello, Avatar!");    }    touch_start(integer total_number)    {        toggle = !toggle;        if(toggle){            llSetTimerEvent(60.0);        }        else {            llSetTimerEvent(0.0);        }    }    timer()    {        vector sun = llGetSunDirection();        if(sun.z>0.0){ // only run the test if the sun is above the horizon            integer i = (integer)llFrand(2.0);//will return 1 or 0            if(i){ //if returns 1                llOwnerSay("True");            }            else{                llOwnerSay("False");            }        }        else{            llOwnerSay("not running test because it's night time");        }    }}

 Note, though, that checking the sun direction is only a rough guide to whether it's light or dark (in my experience it tends to change when sun.z is still somewhere lower than 0.0).   So make sure you do your tests when the sun is high in the sky and when it's clearly dark.   Once you've got it working properly,  we can help you tweak it.

 

Posted


Ela Talaj wrote:

Very material
:)
You may get 20 of 4-s in a row, all with different fractions; they are all less than 5.0 so in a simulator like this a coin would flip the same 20 times in a row. That is not what should be happening.

You've fallen into a variation of what is known as the Gambler's Fallacy, that the coin should somehow "know" it was just heads so should then more likely be tails on the next throw to keep it even. In fact, each coin toss always has a 50/50 chance of heads or tails.

 

Another way to look at it is to realize that to sit down and toss a coin 20 times and have them all come up heads is very unlikely, a chance of only 1 out of 1,048,576. But if it did happen, the 21st toss would be the same as the first was- still 50/50. And, it follows that if one tossed a coin for each dollar of the American Debt, it would be highly unlikely that there wasn't at least one run of twenty heads within it.

 

Having a program alternate between heads and tails would give you 50/50, yes? But you wouldn't consider it random by any means. It's the possibility of those 20 consecutive heads that is intrinsic to the definition.

Posted

The way I see it is that I wrote and tested the fragment I posted above, about 

	integer i = (integer)llFrand(2.0);//will return 1 or 0			if(i){ //if returns 1				llOwnerSay("True");			}			else{				llOwnerSay("False");			}

 in LSLEditor, which was generating floats with 15 decimal places.

I do not see that my casting these floats as integers altered the odds on them being less than 1.0 or not.

Posted

Problems with random numbers? :D It's absolutely ok to use llFrand like in the OP.

To make things clearer just lets make 1 million llFrand calls and count the results:

integer limit = 1000000;

integer count;
integer hi;
integer lo;
integer streak;
integer streakmax;
integer lastresult;

default {
    state_entry() {
    }

    touch_start(integer total_number) {
        llSay(0,"<<< START >>>");
        count = 0;
        lo = 0;
        hi = 0;
        streak = 0;
        streakmax = 0;
        lastresult = -1;
        while (count++ < limit) {
            integer result = llFrand(10.0)<5.0;
            if (result) ++lo; else ++hi;
            
            if (result==lastresult) ++streak;
            else {
                if (streak>streakmax) streakmax=streak;
                streak = 0;
            }
            lastresult = result;
            
            if (!(count % 100000)) llSay(0,"count:"+(string)count+"  lo:"+(string)lo+"  hi:"+(string)hi+" streak:"+(string)streakmax);
        }
        llSay(0,"<<< STOP >>>");
        llSay(0,"count:"+(string)(count-1)+"  lo:"+(string)lo+"  hi:"+(string)hi+" streak:"+(string)streakmax);
    }
}

 As you will see there are about 500000 hits on each side.

I get streaks of 15 to 18 though but I will not do the math to find out if llFrand is a good or bad random number generator. I tend to think it's bad but sufficient for most cases.

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

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
×
×
  • Create New...