0

I'm making a program that infinitely clicks on a certain spot. I made those clicks to be random, using the following code:

public void leftClick() {
    int no = random.nextInt(5) + 1;
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.delay(50 * no);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.delay(250 * no);
}

And here's the loop in which the leftClick() method is used (integer o is initialized above in the program):

while (running) {
        leftClick();            
        o++;
        System.out.println(new Date() + " " + o);
    }

When implemented in my program, this 'clicking' goes on and on, with random pauses between each click. I tested it, and it results in about 35-45 clicks per minute. Is there a way to make my program click for example, 35 times in one minute, and then 70-80 in the next?

eltabo
  • 3,749
  • 1
  • 21
  • 33

4 Answers4

1

Here is one option:

public class Foo {
  long startTime = 0;
  long lastMinCount = 1;
  int multiplier = 50;

  public void leftClick() {
    long currentTime = System.currentTimeMillis();
    if (startTime==0) {
       startTime = currentTime;
    }
    else {
      if (currentTime / startTime > lastMinCount) {
         lastMinCount = currentTime / startTime;
         multiplier = 10 * (random.nextInt(5) + 1);
      }
    }
    int no = random.nextInt(5) + 1;
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.delay(multiplier * no);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.delay(4 * multiplier * no);
  }
}

You can try this

skarist
  • 1,030
  • 7
  • 9
  • It still doesn't do the thing. It tends to increase each minute, which is not desirable, and it's almost always around 80 clicks. – user3830097 Jul 16 '14 at 11:47
  • Well, you can of course decrease the multiplier or the range for the no variable. The multiplier tops out at 50, so I would find it surprising that you are seeing consistently more clicks than in the original. Also, notice that the second delay is shorter than your first one, i.e. tops out at 200 * no, instead of 250 * no – skarist Jul 16 '14 at 11:56
  • Ok, i will try it now. Just a thought, isn't startTime supposed to be equal to 0? – user3830097 Jul 16 '14 at 12:02
  • Yes, just a typo. You can also make the calculation of the multiplier more elaborate of course. Like if (random.nextInt(5) < 2) { multiplier = 10 * (random.nextInt(5) + 1); } else { multiplier = floor(50 / (random.nextInt(5) + 1)) } – skarist Jul 16 '14 at 12:05
0

To get a random number in a particular range you would do something like the following:

Given a range [a,b] (from a to b inclusive) do the following:

  1. interval = b - a + 1
  2. randnumvalue = get random number from [0,0, 1.0)
  3. result = a + interval*randomvalue

To handle the different ranges, save the start time and determine what "minute" you are in and choose the appropriate range to apply the pseudocode above.

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
0

You can do the following

 leftClick(boolean flag){
    int no = random.nextInt(10)
    if(flag == false){
    no = 35+ no;  // This will always give you a number between 35 and 45
    flag = true;
    }else{
    no = 70 + no;
    }
}

so every time you call this method with a boolean true you get no between 35 and 45 otherwise 70-80

Sanyam Goel
  • 2,138
  • 22
  • 40
0

you could do something like this :

public void leftClick(int noOfClicksPerMinute) {
   if(noOfClicksPerMinute >35 && noOfClicksPerMinute < 45){
    int no = random.nextInt(35) + 10;
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.delay(50 * no);//change and set appropriate delay
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.delay(250 * no);//change and set appropriate delay
}

else{

    int no = random.nextInt(70) + 10;
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.delay(50 * no);//change and set appropriate delay
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.delay(250 * no);//change and set appropriate delay
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104