如何在C 程序中随机做一个
how to make a random in c++ program
我想让我的程序选择一个随机数
#include <iostream>
using namespace std;
int main(){
int x=100 y=40;
while (true)
{if(x>y)
{x--;}
else
{break;}}}
,除非您的编译器不支持C 11,否则请使用<random>
库。您需要选择发动机,发动机的种子和分配;如果您不知道如何选择,并且想要一个随机整数,则应该这样做:
#include <random>
#include <iostream>
int main() {
auto engine = std::mt19937(std::random_device()());
auto distribution = std::uniform_int_distribution(0, 10); // between 0 and 10 inclusive
auto random_integer = distribution(engine);
std::cout << random_integer;
}
相关文章:
- 使用std::mt19937从字符串中返回一个随机单词
- 如何生成一个随机的 n 位数,其中 n 是任意的
- 当我尝试使用它时,Scanf 会抛出一个随机异常(scanf_s 也是如此)
- 我将如何在 cpp 中生成一个随机且独特的标题
- 从C++数组中选择一个随机元素
- 如何更改此notify_one以便它选择一个随机线程
- C++ 在运行时选择一个随机对象
- C++ 制作一个随机数字/字母生成器,代码中有问题
- 将一个随机的字符序列和int序列解析为单独的向量
- C 从向量的子集返回一个随机值
- 如何创建一个随机访问式的Rreference以访问.RC文件中定义的资源
- 在C 11中生成一个随机字符串
- 创建一个随机的std ::向量,其中一个和零C
- C/C++ - 生成一个随机字符串会覆盖以前的字符串(请参阅下面的代码)
- 需要在指定范围内生成一个随机整数
- 生成一个随机种子给srand()-不是时间(NULL)
- 选择一个随机方块并生成一个随机数
- 是否有一个随机函数可以生成 0 到 1 之间的数字
- 随机坐标和禁用下一个随机集的移动
- 如何在QtCreator(Linux Ubuntu)中编译和运行一个随机的单个C++文件