Alexey Podoynitsyn
Alexey Podoynitsyn

Reputation: 33

Randomizing coordinates

I am quite new to Python, but I find it is really fun to code

dictionary_of_locations = {

    'location_1': (2214, 1026), # x, y coordinates
    'location_2': (2379, 1016),
    'location_3': (2045, 1092),
    'location_4': (2163, 1080),
    'location_5': (2214, 1080),
    'location_6': (2262, 1078),

} 

I want to run a code that selects the location coordinates and randomizes x and y values by +-15

what exactly I am trying to do is:

for i in dictionary_of_locations.values():
    pyautogui.click(i), print('clicking on location ' + str(i + 1) + ' !'), time.sleep(.75)

Upvotes: 0

Views: 739

Answers (4)

Thomas Kimber
Thomas Kimber

Reputation: 11067

One thing I like about python is how it gently steers people into thinking functionally. So when I see a problem like this, I think - "What single, reusable function would really help in this situation?"

And in answer to that question, I guess some kind of blurrer that, given an input and some parameters, spits out a blurred version. Maybe we want to blurr by a fixed amount (+-15 as per your question) or maybe later, we might want to blur by some amount defined as a ratio.

Here's a starting point that covers the first idea (fixed range of blur), we'll call the input value value and the amount of blur, blur (other naming ideas are available).

Python has a lot of useful randomisation code in its random module, so we'll import that for some of that functionality too.

import random
def blur(value, blur):
    blur_sign = random.choice([-1,1]) # is the blur going to be positive or negative?
    blur_rand = random.randint(0,blur) * blur_sign # pick a random number up to "blur" and multiply by sign
    return value + blur_rand

N.B. use of random.choice in the above is probably a bit on the clunky side, as others have demonstrated here, calling random.randint with a lower-bound, upper-bound as parameters is a cleaner way of getting the range you want and will cross the positive/negative without having to expressly setting it as I've done here.

Try it out, feed the 'blur' function with any value, and some blurring parameter, and it should spit out the kinds of results you want. Now you've got a function that does the job, just use some python to glue it into whatever workflow you're interested in.

An example of how that might look given your existing code:

for x,y in dictionary_of_locations.values():
    i = (blur(x,15), blur(y,15))
    pyautogui.click(i), print('clicking on location ' + str(i + 1) + ' !'), time.sleep(.75)

Upvotes: 0

sagmansercan
sagmansercan

Reputation: 101

You may use numpy.random.randomint()

Example:

import numpy as np
import time

dictionary_of_locations = {

    'location_1': (2214, 1026), # x, y coordinates
    'location_2': (2379, 1016),
    'location_3': (2045, 1092),
    'location_4': (2163, 1080),
    'location_5': (2214, 1080),
    'location_6': (2262, 1078),

}

def get_random(x, y):
    """
    Get (x, y) and return (random_x, random_y) according to rand_range
    +1 is added to high bound because randint is high exclusive [low, high)
    :param x: position x
    :param y: position y
    :return: random values for x and y with range 15
    """
    rand_range = 15  # for -/+ 15

    x_low = x - rand_range
    x_high = x + rand_range + 1
    y_low = y - rand_range
    y_high = y + rand_range + 1
    rand_x = np.random.randint(low=x_low, high=x_high)
    rand_y = np.random.randint(low=y_low, high=y_high)

    return rand_x, rand_y

for location, positions in dictionary_of_locations.items():
    current_x, current_y = positions
    new_x, new_y = get_random(current_x, current_y)
    print(f'clicking on {location} (x, y): ({new_x},{new_y}) !')
    time.sleep(.75)

# output
# clicking on location_1 (x, y): (2209,1040) !
# clicking on location_2 (x, y): (2364,1005) !
# clicking on location_3 (x, y): (2052,1086) !
# clicking on location_4 (x, y): (2160,1092) !
# clicking on location_5 (x, y): (2222,1079) !
# clicking on location_6 (x, y): (2275,1082) !

Upvotes: 0

not-a-feature
not-a-feature

Reputation: 141

You can use randint(lb, ub) to get a random integer in between the lower / upper bound

from random import randint
for loc in dictOfLoc:
    newLoc = (dictOfLoc[loc][0] + randint(-15, 15),
              dictOfLoc[loc][1] + randint(-15, 15))
    dictOfLoc[loc] = newLoc

Upvotes: 0

EnderShadow8
EnderShadow8

Reputation: 1040

Use random.randint:

import random
loc = ... # Do whatever to get your desired coordinates

loc = (loc[0] + random.randint(-15, 15), loc[1] + random.randint(-15, 15))

Or if you don't want just integers but also floats:

import random
loc = ... # Do whatever to get your desired coordinates

loc = (loc[0] + random.random() * 30 - 15, loc[1] + random.random() * 30 - 15)

Upvotes: 1

Related Questions