如何实现rand/ rand?c++
ways to implement a rand/srand? c++
如何在代码中插入rand/srand以使其以随机顺序提问?我对兰德和兰德不太熟悉。我需要的是一个陆地和/或兰德到一个开关柜,但不知道如何使它工作。
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
struct highscore{
int svar;
};
int main(){
ofstream highscore;
bool fail = true;
int lost, fraga, svar = 0; // these are for the Start option
int fraga2; // for srand / rand
int load ,loadh, loadg, tillbaka; // these are for the loading option
string line; // also loading option
char val;
do{
cout << " Menu" << endl;
cout << " 1. Start" << endl;
cout << " 2. Load" << endl;
cout << " 3. Credits" << endl;
cin >> val;
switch(val){
case '1':
highscore.open("Highscore.txt", ios::app);
if (!highscore){
cout << "file could not open";
exit (1);
}
srand(fraga2(time(0));
for (int fraga = 0; fraga< 500; fraga++){
cout << "WELCOME TO THE CISCO QUIZ GOOD LUCK" << endl;
cout << "how many layers are there in the OSI model?" << endl;
cout << "1. 4" << endl;
cout << "2. 5" << endl;
cout << "3. 7" << endl;
cin >> fraga;
switch(fraga){
case 1:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 2:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
svar = svar + 5;
fraga = fraga + 1;
break;
default:
cout << "error" << endl;
}
if(svar == 5){
cout << "what is the full name of OSPF?" << endl;
cout << "1. Open Shortest Path First" << endl;
cout << "2. Online Shooting Pro Fishing" << endl;
cout << "3. On Short Path First" << endl;
cin >> fraga;
switch(fraga){
case 1:
svar = svar + 5;
fraga = fraga +1;
break;
case 2:
cout << "Game Over!"<< endl;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes, 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
}
}
if(svar == 10){
cout << "What does TTL mean?" << endl;
cout << "1. Twin Twitched Life" << endl;
cout << "2. Two Time Lives" << endl;
cout << "3. Time To Live" << endl;
cin >> fraga;
switch(fraga){
case 1:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with yes, YES or Yes)"<< endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 2:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
svar = svar + 10;
fraga = fraga +1;
break;
}
}
if(svar == 20){
cout << "What is the first command you enter on a Cisco router?" << endl;
cout << " 1. enable"<< endl;
cout << " 2. disable" << endl;
cout << " 3. enter" << endl;
cin >> fraga;
switch(fraga){
case 1:
svar = svar + 5;
fraga = fraga +1;
break;
case 2:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
}
}
cout << svar;
if(svar == 25){
cout << "which protocol is the most prefered?" << endl;
cout << " 1. RIP" << endl;
cout << " 2. EGRP" << endl;
cout << " 3. EIGRP" << endl;
cin >> fraga;
switch(fraga){
case 1:
cout << "Game Over!" << endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 2:
cout << "Game Over!"<< endl;
cout << "Do you wish to restart? (answer with 1 for yes and 2 for no)" << endl;
cin >> lost;
if(lost == 2 ){
fail = false;
}
break;
case 3:
svar = svar + 15;
fraga = fraga +1;
cout << "You have completed the game press 1 to exit or if you wish to do it again or check highscores press 2" << endl;
cin >> lost;
if (lost == 1 ){
fail = false;
}
else{
fail = true;
}
break;
}
}
}
highscore <<"Score: " << svar << 'n';
highscore.close();
cin.ignore(10,'n');
cin.get();
case '2':
cout << "type 1 to load game or type 2 to load highscores" << endl;
cin >> load;
if (load == 1){
cout << "in progress";
}
else if(load == 2){
ifstream loadh ("highscore.txt");
if (loadh.is_open())
{
while ( loadh.good() )
{
getline (loadh,line);
cout << line << endl;
}
cout << "do you wish to return to the menu press 1 else press any number" << endl;
if (tillbaka != 1){
fail = false;
}
highscore.close();
}
}
cin.ignore(10,'n');
cin.get();
break;
case '3':
cout << "made by trickjay the most awesome guy on the planet!!!" << endl;
cin.ignore(10,'n');
cin.get();
break;
}
}while(fail);
}
//根据时间戳返回100%保证的随机数int randNumber() {返回213213;}之前你也可以像
那样实现它<>之前//这个也可以int randNumber() {返回rand ();}
是否检查了参考文献中的这些函数?它们被很好地描述了,也有一些例子。在谷歌上搜索答案之前,你应该先从这个开始。
http://en.cppreference.com/w/cpp/numeric/random/srandint num;
for( int i=0; i<3 ; i++ ) {
num = rand() % 3;
switch(num) {
case 0: askone(); break;
case 1: asktwo(); break;
case 2: askthree(); break;
}
}
这将给每个程序运行相同的顺序。如果您想要一些不那么确定的东西,请查看播种。你也可以添加一些机制来避免重复的问题。
与此无关,您应该考虑减少程序中的缩进。通过将一些子任务放入函数中,例如。
另外,您可以将外部do-while循环更改为普通的while循环,而不更改程序的行为。
如果你正在使用支持c++ 11的编译器,那么你有新的random
方法:
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_int_distribution<int> dist(0, 2);
int num;
for( int i=0; i<3 ; i++ ) {
num = dist(e2);
switch(num) {
case 0: askone(); break;
case 1: asktwo(); break;
case 2: askthree(); break;
}
}
对于这种情况,它可能没有太大的区别,但从长远来看,不使用std::rand
有好处,因为它不是线程安全的,rand
生成的随机序列的质量不能保证。
相关文章:
- 为什么我不能将 rand() 与数组的大小一起使用?
- 为什么新的随机库比std::rand()更好
- 为什么rand()的使用被认为是不好的
- C++使用 rand 定义函数语法
- 为什么 rand 不在我的代码中生成随机数?
- 为什么 CWE 认为 rand() 具有潜在危险
- 在 c++ 中,rand() 在执行有调试和不带调试的程序时生成不同的结果
- Rand() 没有显示随机数,请帮助我了解出了什么问题:)
- Rand()大小写开关在多次迭代后运行到无穷大
- rand()函数在特定问题中的工作
- 为什么 someNumber = rand() & 100 + 1;不产生错误?
- 除了 rand() 之外,是否有任何随机生成器库,开发人员可以手动设置种子?
- 如何生成比用户使用 rand() 函数输入的数字大 5-10% 的数字?
- rand()/RAND_MAX 函数每次打印 1
- 使用 rand() 和 srand() 有问题
- " rand()%3-1 " 在 C++ 中是什么意思?
- 如何使 rand() 更加随机?
- C++ 更深入地了解 rand() 和 srand() 内部
- 在 C++ 中向 rand() 添加模数会影响 for 循环之外的执行,为什么以及如何补救?
- cpp中rand()方法的bankend程序是什么?