Jump to content

Easy Sleeping Snippet


Kati2

Recommended Posts

I became annoyed at typing sleep x + gRandom(y,z); so I wrote this so you can simply enter three values (you can change it to more) and it will randomize the sleep method furthermore. Not only that, instead of having to type out your sleep every time, you just type:

Sleep();

Here's the code:

 

private boolean dynamicBase;

    int s0 = 750; // Base value
    int s1 = 0;
    double s2 = 0;
    
    public void subSleep() throws InterruptedException {
        sleep(s0 + gRandom(s1, s2));
    }

    public void sleep() throws InterruptedException {
        
        Random sleepRandom = new Random();
        switch (random(1, 3)) {
            case 1:
                if (dynamicBase) {
                    s0 = sleepRandom.nextInt(500);
                }
                s1 = sleepRandom.nextInt(1000);
                s2 = 250 + (1500 - 500) * sleepRandom.nextDouble();
                subSleep();
            case 2:
                if (dynamicBase) {
                    s0 = sleepRandom.nextInt(100);
                }
                s1 = sleepRandom.nextInt(250);
                s2 = 500 + (800 - 200) * sleepRandom.nextDouble();
                subSleep();
            case 3:
                if (dynamicBase) {
                    s0 = sleepRandom.nextInt(45);
                }
                s1 = sleepRandom.nextInt(250);
                s2 = 500 + (800 - 200) * sleepRandom.nextDouble();
                subSleep();
        }
    }
 

Pretty much, your sleep is now: randomBase(optionally) + randomint(randomint + (randomint(randomint)) + (randomDouble(randomDouble))) 

 

Making your sleeps math-ambiguous.

  • Like 2
Link to comment
Share on other sites

I think you would be much better off using this (quick writeup)

public void newSleep(int min, int max) {
   int x = 0;
   do {
      x = gRandom(min,max,(double)(max-min)/2);
   } while (x < min || x > max);
   sleep(x);
}
This way you can refine it in certain areas of your code, instead of it being generally around one area. Edited by dreamliner
Link to comment
Share on other sites

 

l2 use conventions tongue.png

 

Happy? tongue.png

 

So how would you use this.

Sleep(500+200,300);

 

If you want to do that just use the normal method..

 

why not use dynamic sleeps?

import timer.Timer;
import org.osbot.script.Script;

import org.osbot.script.rs2.model.Player;

public class Singleton {


    private final  Script script;
    private static Singleton oneInstance;

    private  Singleton(Script script){
             this.script = script;
    }

    public static Singleton getInstance(final Script script){
        if(oneInstance == null){
            oneInstance = new Singleton(script);
        }
        return oneInstance;
    }



    public boolean isIdle(){
        Player p = script.client.getMyPlayer();
        return !p.isAnimating() && p.getAnimation()==-1 && !p.isMoving() && !p.isUnderAttack();
    }

       public void dynamicSleep(final boolean condition){
         Timer  timer = new Timer(800);
        try{
            while(!isIdle() && condition && timer.isRunning())
                script.sleep(10);
        }
        catch (InterruptedException e){e.printStackTrace();}
          
       }

   }












Link to comment
Share on other sites

I think you would be much better off using this (quick writeup)

public void newSleep(int min, int max) {
   int x = 0;
   do {
      x = gRandom(min,max,(double)(max-min)/2);
   } while (x < min || x > max);
   sleep(x);
}
This way you can refine it in certain areas of your code, instead of it being generally around one area.

 

 

 

That's not as randomized as the method I posted, nor is it as simplified. 

 

 

 

l2 use conventions tongue.png

 

Happy? tongue.png

 

So how would you use this.

Sleep(500+200,300);

 

If you want to do that just use the normal method..

 

why not use dynamic sleeps?

import timer.Timer;
import org.osbot.script.Script;

import org.osbot.script.rs2.model.Player;

public class Singleton {


    private final  Script script;
    private static Singleton oneInstance;

    private  Singleton(Script script){
             this.script = script;
    }

    public static Singleton getInstance(final Script script){
        if(oneInstance == null){
            oneInstance = new Singleton(script);
        }
        return oneInstance;
    }



    public boolean isIdle(){
        Player p = script.client.getMyPlayer();
        return !p.isAnimating() && p.getAnimation()==-1 && !p.isMoving() && !p.isUnderAttack();
    }

       public void dynamicSleep(final boolean condition){
         Timer  timer = new Timer(800);
        try{
            while(!isIdle() && condition && timer.isRunning())
                script.sleep(10);
        }
        catch (InterruptedException e){e.printStackTrace();}
          
       }

   }












 

As I said, I was trying to make it as random as possible.

Link to comment
Share on other sites

I became annoyed at typing sleep x + gRandom(y,z); so I wrote this so you can simply enter three values (you can change it to more) and it will randomize the sleep method furthermore. Not only that, instead of having to type out your sleep every time, you just type:

Sleep();

Here's the code:

 

private boolean dynamicBase;

    int s0 = 750; // Base value
    int s1 = 0;
    double s2 = 0;
    
    public void subSleep() throws InterruptedException {
        sleep(s0 + gRandom(s1, s2));
    }

    public void sleep() throws InterruptedException {
        
        Random sleepRandom = new Random();
        switch (random(1, 3)) {
            case 1:
                if (dynamicBase) {
                    s0 = sleepRandom.nextInt(500);
                }
                s1 = sleepRandom.nextInt(1000);
                s2 = 250 + (1500 - 500) * sleepRandom.nextDouble();
                subSleep();
            case 2:
                if (dynamicBase) {
                    s0 = sleepRandom.nextInt(100);
                }
                s1 = sleepRandom.nextInt(250);
                s2 = 500 + (800 - 200) * sleepRandom.nextDouble();
                subSleep();
            case 3:
                if (dynamicBase) {
                    s0 = sleepRandom.nextInt(45);
                }
                s1 = sleepRandom.nextInt(250);
                s2 = 500 + (800 - 200) * sleepRandom.nextDouble();
                subSleep();
        }
    }
 

Pretty much, your sleep is now: randomBase(optionally) + randomint(randomint + (randomint(randomint)) + (randomDouble(randomDouble))) 

 

Making your sleeps math-ambiguous.

Kati! Finally Yay A Snippet :D

Thanks! I Will Be Using This! :D

Link to comment
Share on other sites

 

I think you would be much better off using this (quick writeup)

public void newSleep(int min, int max) {
   int x = 0;
   do {
      x = gRandom(min,max,(double)(max-min)/2);
   } while (x < min || x > max);
   sleep(x);
}
This way you can refine it in certain areas of your code, instead of it being generally around one area.

 

 

 

That's not as randomized as the method I posted, nor is it as simplified. 

I'd have to disagree.  I did a little bit of statistical analysis on your method:

 

Case one yields an average range of 750 to 2000 if not using a dynamic base. 

Case one yields an average range of 250 to 1500 if using a dynamic base. 

 

Case two yields an average range of 750 to 1675 if not using a dynamic base.

Case two yields an average range of 50 to 975 if using a dynamic base.

 

Case three yields an average range of 750 to 1675 if not using a dynamic base.

Case three yields an average range of 23 to 948 if using a dynamic base.

 

(all of these are bell curves with their peak at the mean)

 

If you logged every time the bot slept, you would have a graph that looks like 2 or 3 superimposed bell curves (or 5 if you randomly use the dynamic base).

 

To have something truly random, you have to use this method along with a way of changing the base to better suit your needs in a script.  Such as an action that requires 1 tick compared to something that needs 5.  If you slept the same way every time, it would look obvious by the randomness of it.

 

By default, humans are not random.  There was a study done that asked a human to flick on and off a light switch in a random pattern.  The study concluded that there was a non-random pattern that the person followed subconsciously.  When humans do actions, they get tuned to what they are doing.  It is very easy to keep the beat in a song, compared to trying to play something in random intervals.  Its not how our brains work.

 

This is a very well written video explaining what I'm walking about http://www.youtube.com/watch?v=H2lJLXS3AYM

Edited by dreamliner
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...