简体   繁体   中英

Random time delay non-integer

I am trying to create a random delay between 0.05634 and 0.15342 I've tried looking at other posts but they all use integer numbers and these numbers aren't hope to see if someone can help:)

Ive tried using the random function and the time.sleep function but nothing seems to work!

Im trying to make a double clicker for a game but when I use the random function it lags everytime I press the mouse button, When I remove the time.sleep function it works without anylag!

from pynput.keyboard import Key, Listener
import pyautogui
from pynput import keyboard
from pynput.mouse import Controller
from pynput.mouse import Listener
import time
import random

pyautogui.PAUSE = 0.001

random = random.random()
mouse = Controller()


def on_click(x, y, button, pressed):
    time.sleep(random)
    pyautogui.press('home')



with Listener(
        on_click=on_click) as listener:
    listener.join()

to answer the question in the title;

import random
import time
time_delay=random.uniform(0.05634,0.15342)
time.sleep(time_delay)

however, regarding the lagging time delay, instead of using a standard time delay, have you tried creating your own, maybe replacing the time.sleep line with for i in range(10000000): x=0 , this will create a halt in your program that is different every time, as you can see below;

>>> timeit.timeit('for i in range(10000000): x=0',number=1)
0.20446060000000443
>>> timeit.timeit('for i in range(10000000): x=0',number=1)
0.19383630000001517
>>> timeit.timeit('for i in range(10000000): x=0',number=1)
0.2523204000000021
>>> timeit.timeit('for i in range(10000000): x=0',number=1)
0.18888549999999782

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM