stl mt19937
时间: 2023-09-27 19:05:25 浏览: 127
`std::mt19937` 是 C++ 标准库中的一个伪随机数生成器。它是 Mersenne Twister 算法的一个实现,可以生成高质量的随机数序列。
您可以使用以下代码来使用 `std::mt19937`:
```cpp
#include <iostream>
#include <random>
int main() {
// 创建一个 mt19937 对象并初始化种子
std::mt19937 rng(std::random_device{}());
// 生成随机整数
std::uniform_int_distribution<int> dist(1, 100);
int random_number = dist(rng);
std::cout << "随机数: " << random_number << std::endl;
return 0;
}
```
在上面的示例中,我们首先创建了一个 `std::mt19937` 对象 `rng` 并通过 `std::random_device` 来初始化种子。然后,我们使用 `std::uniform_int_distribution` 来定义生成随机整数的范围,并使用 `dist(rng)` 来生成随机数。
希望这可以解答您的问题!如果您还有其他问题,请随时提问。
相关问题
mt19937的用法
在C++ STL中,可以通过 `<random>` 头文件中的 `mt19937` 类来实现MT19937算法的使用。
下面是一个简单的示例代码,演示如何生成一个随机数序列:
```c++
#include <random>
#include <iostream>
int main() {
std::random_device rd; // 获取一个随机设备
std::mt19937 gen(rd()); // 使用随机设备生成种子值
std::uniform_int_distribution<> distrib(1, 6); // 生成随机数分布
for (int i = 0; i < 10; ++i) {
std::cout << distrib(gen) << ' '; // 生成随机数并输出
}
return 0;
}
```
在这个示例代码中,我们首先使用 `std::random_device` 来获取一个随机设备,然后使用 `std::mt19937` 类来生成一个MT19937算法的随机数生成器,使用随机设备生成种子值。接下来,我们使用 `std::uniform_int_distribution` 类来生成一个在1到6之间均匀分布的随机数分布,并且通过随机数生成器来生成随机数。
在这个示例代码中,我们生成了10个1到6之间的随机数,并且将它们输出到屏幕上。你可以根据需要修改随机数的分布和生成的随机数个数。
#include <iostream>#include <random>#include <string>#include <algorithm>#include <cctype>using namespace std;const int MAX_WRONG = 6;const string WORDS[] = {"HELLO", "WORLD", "COMPUTER", "PROGRAMMING", "LANGUAGE"};const int NUM_WORDS = sizeof(WORDS) / sizeof(string);int main() { random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dis(0, NUM_WORDS - 1); char playAgain = 'y'; while (tolower(playAgain) == 'y') { string word = WORDS[dis(gen)]; int wrongGuesses = 0; string guessedSoFar(word.size(), '_'); string lettersGuessed; cout << "Welcome to Hangman. Good luck!\n\n"; while (wrongGuesses < MAX_WRONG && guessedSoFar != word) { cout << "You have " << MAX_WRONG - wrongGuesses << " incorrect guesses left.\n"; cout << "You've used the following letters:\n" << lettersGuessed << endl; cout << "So far, the word is:\n" << guessedSoFar << endl; char guess; cout << "\nEnter your guess: "; cin >> guess; guess = toupper(guess); if (lettersGuessed.find(guess) != string::npos) { cout << "You've already guessed " << guess << endl; continue; } lettersGuessed += guess; if (word.find(guess) != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; for (int i = 0; i < word.size(); ++i) { if (word[i] == guess) { guessedSoFar[i] = guess; } } } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrongGuesses; } } if (wrongGuesses == MAX_WRONG) { cout << "\nYou've been hanged!"; } else { cout << "\nYou guessed it!"; } cout << "\nThe word was " << word << endl; cout << "Do you want to play again? (y/n): "; cin >> playAgain; } return 0;}优化这段代码
以下是对这段代码的一些优化建议:
1. 对于随机数生成器的使用,可以将其封装成一个函数,减少代码重复。
2. 可以将常量 MAX_WRONG 和 WORDS[] 定义为 const 类型,避免被误修改。
3. 使用 range-based for 循环来遍历 lettersGuessed 字符串。
4. 可以将判断 guess 是否已经猜过的代码抽象成一个函数。
5. 对于字符串的查找和替换操作,可以使用 STL 中提供的相应函数,如 find() 和 replace(),而不是手动实现。
6. 对于输入的字符,可以先使用 cin.get() 获取一个字符,再使用 toupper() 函数将其转换成大写字母。
7. 可以将游戏逻辑封装成一个函数,提高代码复用性。
下面是优化后的代码:
```
#include <iostream>
#include <random>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
const int MAX_WRONG = 6;
const string WORDS[] = {"HELLO", "WORLD", "COMPUTER", "PROGRAMMING", "LANGUAGE"};
const int NUM_WORDS = sizeof(WORDS) / sizeof(string);
// 生成随机数
int getRandomNumber(int min, int max) {
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(min, max);
return dis(gen);
}
// 判断字符是否已经猜过
bool isGuessAlreadyMade(char guess, const string& lettersGuessed) {
return (lettersGuessed.find(guess) != string::npos);
}
// 猜词游戏逻辑
void playGame() {
string word = WORDS[getRandomNumber(0, NUM_WORDS - 1)];
int wrongGuesses = 0;
string guessedSoFar(word.size(), '_');
string lettersGuessed;
cout << "Welcome to Hangman. Good luck!\n\n";
while (wrongGuesses < MAX_WRONG && guessedSoFar != word) {
cout << "You have " << MAX_WRONG - wrongGuesses << " incorrect guesses left.\n";
cout << "You've used the following letters:\n";
for (char letter : lettersGuessed) {
cout << letter << " ";
}
cout << endl;
cout << "So far, the word is:\n" << guessedSoFar << endl;
char guess;
cout << "\nEnter your guess: ";
cin.get(guess);
guess = toupper(guess);
if (isGuessAlreadyMade(guess, lettersGuessed)) {
cout << "You've already guessed " << guess << endl;
continue;
}
lettersGuessed += guess;
if (word.find(guess) != string::npos) {
cout << "That's right! " << guess << " is in the word.\n";
replace(guessedSoFar.begin(), guessedSoFar.end(), '_', guess);
} else {
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrongGuesses;
}
}
if (wrongGuesses == MAX_WRONG) {
cout << "\nYou've been hanged!";
} else {
cout << "\nYou guessed it!";
}
cout << "\nThe word was " << word << endl;
}
int main() {
char playAgain = 'y';
while (tolower(playAgain) == 'y') {
playGame();
cout << "Do you want to play again? (y/n): ";
cin >> playAgain;
}
return 0;
}
```
阅读全文