骰子卷不工作C ++ deafult_random_engine [英] Dice roll not working C++ deafult_random_engine

查看:242
本文介绍了骰子卷不工作C ++ deafult_random_engine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我每次都得到 6 。我知道另一种做随机骰子卷的方法,但我想学习如何使用 deafult_random_engine

For some reason I keep getting 6 every time. I know of another way to do a random dice roll, but I wanted to learn how to use the deafult_random_engine.

#include <iostream>
#include <string>
#include <random>
#include <ctime>

using namespace std;

int main()
{
    default_random_engine randomGenerator(time(0));
    uniform_int_distribution<int> diceRoll(1, 6);

    cout << "You rolled a " << diceRoll(randomGenerator) << endl;
}

但这一段代码适用于 0)

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
// dice roll
{
    srand(time(0));
    for(int x = 1; x < 2; x++){
        cout << 1+(rand()%6) << endl;
    }
    return 0;
}


推荐答案

 #include <iostream>
 #include <string>
 #include <random>
 #include <ctime>

 using namespace std;

 int main()
 {
  mt19937 randomGenerator(time(0));
  uniform_int_distribution<int> diceRoll(1, 6);

  cout << "You rolled a " << diceRoll(randomGenerator) << endl;
 }

这篇关于骰子卷不工作C ++ deafult_random_engine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆