How to generate a true random number from mouse movement?

1.5k Views Asked by At

How to create a python program that lets users move the mouse anywhere on the screen and generate a random number based on those movements. I am trying to implement this feature

1

There are 1 best solutions below

8
LevB On

You don't need to reinvent the wheel - random.random does a good job in generating random numbers. You just need to re-seed it before each random call. Below, a complex number that represents the mouse position is used as a seed. Of course, the same mouse position will generate the same number.

import pyautogui
import random as r

def rnd_mouse():
    pos = pyautogui.position()
    sd = pos[0]+1j*pos[1]
    r.seed(sd)
    return r.random()


print(rnd_mouse())

Output:

 0.25073497760281793