-3

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

martineau
  • 119,623
  • 25
  • 170
  • 301
bashkash
  • 47
  • 7

1 Answers1

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
  • The real question is that will this number be true random number? – bashkash Jul 24 '20 at 14:18