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
Asked
Active
Viewed 1,351 times
-3
-
What have you tried? StackOverflow is not a coding service. https://stackoverflow.com/help/how-to-ask – alec_djinn Jun 28 '20 at 15:43
-
@alec_djinn I just wanted a guideline as to how to implement it and what is the logic behind it – bashkash Jun 28 '20 at 18:35
-
If you want truly random numbers, use `random.SystemRandom`, eg `rng = random.SystemRandom(); x = rng.randint(1, 10)`. – Steven Soroka Nov 16 '21 at 21:44
1 Answers
1
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

LevB
- 925
- 6
- 10
-
what is the purpose of r.seed() and seed() function in general.Also, will it be true random like the website I mentioned.Thanks – bashkash Jun 28 '20 at 18:37
-
https://stackoverflow.com/questions/22639587/random-seed-what-does-it-do These will be truly random as long as you don't click on the same spot. – LevB Jun 28 '20 at 20:20
-
can you tell me why did you use 1j*pos[1] here. I read it is for imaginary numbers. Thanks – bashkash Jun 28 '20 at 21:16
-
The x-axis is real, the y-axis is imaginary (1j*pos[1]). This way, both axes of your mouse position are used in computing the random number. – LevB Jun 28 '20 at 22:21
-
Lev B I am confused as to why is the y co-ordinate on a computer screen imaginary can you please guide. – bashkash Jun 29 '20 at 09:01
-
https://math.stackexchange.com/questions/2660523/why-are-complex-numbers-treated-as-coordinates – LevB Jun 29 '20 at 16:49
-
https://www.khanacademy.org/math/algebra2/x2ec2f6f830c9fb89:complex/x2ec2f6f830c9fb89:complex-plane/a/the-complex-plane – LevB Jun 29 '20 at 17:57
-