代码示例

#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

int main()
{
    srand(time(NULL)); // 随机数种子,以当前时间作为参数
    int count = 0; // 计数器,统计尝试次数
    while (true)
    {
        float probability = (float) rand() / RAND_MAX; // 生成概率值
        if (probability <= 0.00199)
        {
            // 如果概率小于等于0.00199,即0.199%
            cout << "Congratulations! Your equipment has been successfully transformed!" << endl;
            cout << "Total attempts: " << count << endl; // 输出尝试次数
            break;// 结束循环
        }
        count++;// 尝试次数加1
    }
    return 0;
}
最后修改:2023 年 08 月 04 日
如果觉得我的文章对你有用,请随意赞赏