首页 > 解决方案 > 如何使用 rand 随机化模拟点击

问题描述

我正在用 C++ 制作一个自动点击器它可以工作,但是,我正在尝试使用 rand 函数中的最小和最大整数来随机化它。

它随机化,但它从未真正超过 12 cps。我是 C++ 新手,我来自 C#。

这是我的所有代码:

#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;
}

任何帮助表示赞赏,谢谢!有人请帮助我使用随机发生器,它应该在几天内完成。

标签: c++random

解决方案


这将根据您的需要休眠 1 到 20 之间的随机值:

#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;
}

推荐阅读