gpt4 book ai didi

C++用户定义的随机数生成器

转载 作者:行者123 更新时间:2023-11-28 00:20:02 26 4
gpt4 key购买 nike

我在生成用户定义范围内的 10 个随机数时遇到了一些问题。

到目前为止我有这个:

int minimum, maximum, randNum;

cout << "Hello, please insert the smallest number for your range of random numbers" << endl;
cin >> minimum;
cout << "Please insert your largest number for your range of random numbers" << endl;
cin >> maximum;

srand (time(NULL));
randNum = rand()%(maximum-minimum)+minimum;

不太确定从这里去哪里。感谢任何帮助,谢谢!

最佳答案

给你:

#include <iostream>
#include <random>

int main() {
int minimum, maximum;
std::cin >> minimum >> maximum;

// Create a random number generator
std::random_device rd;
std::mt19937 gen(rd());

// We want random numbers within [minimum, maximum]
std::uniform_int_distribution<> dis(minimum, maximum);

// Print 10 random numbers
for (int i=0; i<10; ++i)
std::cout << dis(gen) << "\n";
}

关于C++用户定义的随机数生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27995882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com