I'm having trouble wrapping my head around random number generation so I figure it would be better to ask now, rather than screw up later.

Let's say I want to write a function that generates a random lucky number from 0-9 when the user clicks on a button.

I'm thinking of using the last digit of the multiple of the x and y coordinates of the click position as the output.

Assuming that the user have no intention of gaming the output, would this be true randomness because there's no way for a 3rd party to predict where on the button the user will click next?

  • 3
    $\begingroup$ Don't do it. Use a random number generator supplied by the system. $\endgroup$ – Yuval Filmus Mar 22 '18 at 8:34

Usually, mouse click position is highly correlated. Just as an example, take a look to a typical mouse interaction with a browser:

Heatmap of mouse click positions

So, the use of raw mouse click positions without pre-whitening is discouraged.


Using the system clock is better than mouse position. Use the exact time (as high precision as is available) as the seed for a random number generator, not the system time itself. Something like

int lucky = new Random(System.currentTime()).nextInt(10);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.