如何使随机数(在50毫秒以下)不会重复两次

问题描述 投票:0回答:1

我正在制作自动点击程序,它会以毫秒为单位生成随机数(范围20-100),假设它会生成延迟:30、40、42(全部

现在,我想使第4个延迟不能再次小于50,我尝试了这个:

我尝试使用for循环,但是当我使用此代码时,autoclicker根本不再起作用/它将无法切换或执行任何操作。

假设您制作了autoclicker脚本,并且它会生成20-100之间的随机数,并且您不希望4行中的延迟小于50,那么您会怎么做?谢谢。

        for (int i = 0; i < 4;)
        {

            // Total Delay aka delay1+delay2
            if (totaldelay < 50)
            {
                i++;
            }

            // Should make totaldelay > 50
            if (i == 3)
            {
                delay1 = RandomInt(75, 105);
            }

            // Reset to 0 so it checks from 0 again
            if (total > 50)
            {
                i = 0;
            }





        }
c++ variables random
1个回答
2
投票

如果我正确理解了这个问题,则您不希望四个连续的数字都小于50。您可以通过简单地保持计数并调整行为来实现此目的,如果前三个都是全部,则生成另一个数字少于50。

如果您正在寻找一个独立的C ++函数来做到这一点(给您每个调用一个随机数,并带有特定的附加限制),则可以使用类似的方法:

int getMyRand() {
    // Keep track of how many consecutive under-50s already done.
    // Careful in multi-threaded code, may need thread-local instead.

    static int consecUnder50 = 0;
    int number;

    // If last three were all < 50, force next one to be >= 50.

    if (consecUnder50 == 3) {
        number = rand() % 51 + 50;  // 50-100 (inclusive).
    } else {
        number = rand() % 81 + 20;  // 20-100 (inclusive).
    }

    // If less, record it, otherwise restart count.

    if (number < 50) {
        ++consecUnder50;
    } else {
        consecUnder50 = 0;
    }

    // Give caller the number.

    return number;
}

[在使用此函数之前,请不要忘记为随机数生成器添加种子,并且要知道C ++具有更好的随机数生成器,尽管rand()通常很好,除非您是统计学家或密码学家:-)

© www.soinside.com 2019 - 2024. All rights reserved.