3

I have wrote a simple auto-clicking script and it works fine, as in it clicks every time i believe it should. One thing i am wondering about it though, It is meant to be delayed on a random interval between 2500 milliseconds and 5000 milliseconds. I am just not 100% it is doing that?

All of the code is here:

public static void click(int desiredAmount)
    {
        int counter = 0;
        Random rand = new Random();

        while (counter < desiredAmount)
        {
            try {
                Thread.sleep(rand.nextInt(5000-2500) + 2500);
            } catch (InterruptedException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }

            robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
            ++counter;
        }
    }

just a simple method that does the clicking, my concern is raised on this line of code Thread.sleep(rand.nextInt(5000-2500) + 2500);

I am using the nextInt(int x) method to get a random INT between two values, but the Thread.sleep(); method I'm using takes a long as a param. Does this mean it will truncate or something? instead of it being 2500 milliseconds it will just be 2 seconds? or 3 seconds or 4 seconds instead of 2646 milliseconds or 3876 milliseconds etc?

Is it actually delaying each click by a random millisecond from 2500-5000? I'm having a hard time figuring that out.

SenjuXo
  • 157
  • 10
  • The "integer"ness refers to the number of whole milliseconds, not the number of whole seconds, so passing an argument of `(int) 2345` will result in a sleep of 2345ms (give or take). – Andy Turner Jan 11 '16 at 00:03
  • So then yes it is delaying each click by a random millisecond? I ask because i was trying to test to see if the time in-between each click is actually varying – SenjuXo Jan 11 '16 at 00:05
  • My goal is to avoid having a pattern to it. Is Java's Random class good enough to do that? I know it is not truly random – SenjuXo Jan 11 '16 at 00:08
  • Yes. Java's Random class is good enough. Nothing computer generated is truly random, but unless you are doing crypto, you don't need to worry about it. – Lucien Stals Jan 11 '16 at 00:49

1 Answers1

3

The delay value will be a random number from 2500 to 4999, since nextInt upper bound is exclusive. From the Java SE Specification:

public int nextInt(int bound)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All bound possible int values are produced with (approximately) equal probability.

rand.nextInt(5000-2500) + 2500 is the same as rand.nextInt(2500) + 2500 (which in my opinion is a cleaner way to write it).

Also int values are converted to long without losing information (long has a wider range than int so no problems there). From the Java SE Specification:

A conversion from type int to type long requires run-time sign-extension of a 32-bit integer value to the 64-bit long representation. No information is lost.

Finally about random not being truly random, you are right it's pseudo-random (which means that all posible numbers has not an exact equal probability, but an approximately equal one). I'm not completely sure what are you using this code for, but I think this will be "random enough" for your needs.

You can have a look at this post about random values in java between a given range. And this post about Random randomness.

PD: A nice improvement in your code (imho) would be making the rand variable private static instead of local.

Community
  • 1
  • 1
marian0
  • 664
  • 6
  • 15