1

i am making an autoclicker in c++ It works, but, i am trying to randomize it using a min and max integer in the rand function.

It randomizes, but it never really goes above 12 cps. I'm new to c++, i came from c#.

Here is all my code:

#include <iostream>
#include<Windows.h>
#include<stdlib.h>
#include <random>
using namespace std;


int x = 0, y = 0, Mincps, Maxcps, randomized_cps;
bool click = false;
int randomize_cps(int min, int max);

void Menu()
{
    system("color 5");
    cout << "Minimum CPS: ";
    cin >> Mincps;

    system("CLS");

    cout << "Maximum CPS: ";
    cin >> Maxcps;
    system("CLS");

    if (Mincps > 20 || Maxcps > 20 || Mincps < 1 || Maxcps < 1)
    {
        cout << "That CPS is not safe" << endl;
        Sleep(1000);
        system("CLS");
        Menu();
    }

    cout << "AirClicker\n";
    cout << "Made by Deagan";
    Sleep(1500);

    system("CLS");

    cout << "Minimum CPS: ";
    cout << Mincps;

    cout << "\n\n";

    cout << "Maximum CPS: ";
    cout << Maxcps;

    cout << "\n\n";

    cout << "Press X to toggle on and Z to toggle off.\n";
}

void Clicker()
{

    while (1)
    {
        if (GetAsyncKeyState('X'))
        {
            click = true;
        }

        if (GetAsyncKeyState('Z'))
        {
            click = false;
        }

        if (click == true)
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
            Sleep(1000 / randomized_cps);
        }
    }
}

int main()
{
    Menu();
    Clicker();
}

int randomize_cps(int min, int max)
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> distr(Mincps, Maxcps);
    randomized_cps = distr(gen);
    return 0;
}

Any help is appreciated, thanks! Someone please help me with the randomizer, it's supposed to be done in a few days.

Deagan Muir
  • 103
  • 4
  • 13
  • 1
    I would suggest you 2 things: 1- read https://stackoverflow.com/questions/322938/recommended-way-to-initialize-srand , otherwise your rand will return always the same values. 2 - Provide the input you use for your application (eg: max, min value) – Stefano Buora Jun 18 '20 at 15:21
  • 3
    There's no call to `srand()`. But I find it interesting that you have `#include ` and then don't use the superior random number facilities that it provides. – Fred Larson Jun 18 '20 at 15:21
  • 2
    If you want the duration between clicks to be randomized, you need to make this random calculation _per click_ not once when the program starts. – cdhowie Jun 18 '20 at 15:42
  • @cdhowie Thank you! i figured it out, but there is one issue. The CPS usually stays around the min cps and doesn't go up to the max much. I'm using instead of rand now. – Deagan Muir Jun 18 '20 at 15:56
  • @DeaganMuir Update the code in this question with your new code. – cdhowie Jun 18 '20 at 16:33
  • @cdhowie done :) – Deagan Muir Jun 18 '20 at 16:55
  • Due to rounding with `1000 / randomized_cps`, it may end up clicking more than the maximum, and will almost never be the minimum. For inputs of 30cps-60cps, that will result in waits of 33 to 16ms, which are 30.3-62.5cps. – Mooing Duck Jun 18 '20 at 17:00
  • @MooingDuck that's fine, i'm just trying to make the randomization better. – Deagan Muir Jun 18 '20 at 17:11
  • I suspect the problem might actually be a lack of delay between `MOUSEEVENTF_LEFTDOWN` and `MOUSEEVENTF_LEFTUP`? So maybe only every other click is getting processed? Since you limit inputs to 20, that would effectively be ~10cps, which is close to what you're observing. I would add logging to the clickdown/clickup with timestamps, to verify how often your code runs. – Mooing Duck Jun 18 '20 at 18:32

1 Answers1

0

This will sleep for a random value between 1 and 20 as you wanted:

#include <Windows.h>
#include <iostream>
#include <random>

int main()
{
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1, 20);

    while (1)
    {
        int random_value = distribution(generator);
        std::cout << random_value << std::endl;
        Sleep(random_value); // this is already in milliseconds
    }

    return 0;
}
GuidedHacking
  • 3,628
  • 1
  • 9
  • 59