随机数生成器以升序输出数字
Random Number generator to output numbers in ascending order
首先,我是一名初级程序员,希望得到一些帮助。
我编写了以下代码,这些代码是我测试生成的:
- 5个介于1和39之间的随机数 //num1gen to num5gen - e.g.group A
- 1 之间的随机数:1 到 14 //Thunderball - e.g.group B
我只想按升序计算<< A 组数字,我不确定添加此功能需要什么编码。
我还需要将生成的随机数放在如下所示的整数名称中:
即
- num1gen = 12
- num2gen = 24
- num3gen = 3
- num4gen = 5
- num5gen = 32
- 雷球 = 12
错误:ISO C++ 禁止在第 16 行没有类型 [-fpremissive] 的 'i' 减速。
到目前为止我在以下用户的帮助下完成的代码:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
srand(time(NULL));
std::vector<int> numA(5);
srand( time(NULL) );
for( auto i(0); i < numA.size(); ++i ) //line no 16 error
numA[i] = (rand()%49+1);
int num1gen=(rand()%49+1); // this is the value of ball no.1
int num2gen=(rand()%49+1); // this is the value of ball no.2
int num3gen=(rand()%49+1); // this is the value of ball no.3
int num4gen=(rand()%49+1); // this is the value of ball no.4
int num5gen=(rand()%49+1); // this is the value of ball no.5
std::sort(numA.begin(), numA.end());
num1gen=numA[0];
num2gen=numA[1];
num3gen=numA[2];
num4gen=numA[3];
num5gen=numA[4];
cout<<num1gen<< ", "<<num2gen<< ", "<<num3gen<< ", "<<num4gen<< ", "
<<num5gen<< " ";"
return 0;
}
如果您将 A 中的数字创建为向量,则有一个带排序的算法 header,例如:
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numA(5);
srand( time(NULL) );
for( unsigned int i(0); i < numA.size(); ++i )
numA[i] = (rand()%49+1);
//After you create the vector and do your test that they're not equal
std::sort(numA.begin(), numA.end());
return 0;
}
std::sort()
包含在 #include <algorithm>
header 中。
#include <random>
#include <vector>
#include <algorithm>
#include <numeric>
int main()
{
std::vector<unsigned> balls(39);
std::iota(balls.begin(), balls.end(), 1);
std::shuffle (foo.begin(), foo.end(), std::mt19937(std::random_devic{}()));
balls.resize(5);
std::sort(balls.begin(), balls.end());
std::cout << "Balls: "
std::copy(balls.begin(), balls.end(), std::ostream_iterator<unsigned>{std::cout," "})
}
首先,我是一名初级程序员,希望得到一些帮助。
我编写了以下代码,这些代码是我测试生成的:
- 5个介于1和39之间的随机数 //num1gen to num5gen - e.g.group A
- 1 之间的随机数:1 到 14 //Thunderball - e.g.group B
我只想按升序计算<< A 组数字,我不确定添加此功能需要什么编码。
我还需要将生成的随机数放在如下所示的整数名称中:
即
- num1gen = 12
- num2gen = 24
- num3gen = 3
- num4gen = 5
- num5gen = 32
- 雷球 = 12
错误:ISO C++ 禁止在第 16 行没有类型 [-fpremissive] 的 'i' 减速。
到目前为止我在以下用户的帮助下完成的代码:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
srand(time(NULL));
std::vector<int> numA(5);
srand( time(NULL) );
for( auto i(0); i < numA.size(); ++i ) //line no 16 error
numA[i] = (rand()%49+1);
int num1gen=(rand()%49+1); // this is the value of ball no.1
int num2gen=(rand()%49+1); // this is the value of ball no.2
int num3gen=(rand()%49+1); // this is the value of ball no.3
int num4gen=(rand()%49+1); // this is the value of ball no.4
int num5gen=(rand()%49+1); // this is the value of ball no.5
std::sort(numA.begin(), numA.end());
num1gen=numA[0];
num2gen=numA[1];
num3gen=numA[2];
num4gen=numA[3];
num5gen=numA[4];
cout<<num1gen<< ", "<<num2gen<< ", "<<num3gen<< ", "<<num4gen<< ", "
<<num5gen<< " ";"
return 0;
}
如果您将 A 中的数字创建为向量,则有一个带排序的算法 header,例如:
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numA(5);
srand( time(NULL) );
for( unsigned int i(0); i < numA.size(); ++i )
numA[i] = (rand()%49+1);
//After you create the vector and do your test that they're not equal
std::sort(numA.begin(), numA.end());
return 0;
}
std::sort()
包含在 #include <algorithm>
header 中。
#include <random>
#include <vector>
#include <algorithm>
#include <numeric>
int main()
{
std::vector<unsigned> balls(39);
std::iota(balls.begin(), balls.end(), 1);
std::shuffle (foo.begin(), foo.end(), std::mt19937(std::random_devic{}()));
balls.resize(5);
std::sort(balls.begin(), balls.end());
std::cout << "Balls: "
std::copy(balls.begin(), balls.end(), std::ostream_iterator<unsigned>{std::cout," "})
}