【问题标题】:Random Number generator to output numbers in ascending order随机数生成器以升序输出数字
【发布时间】:2015-02-20 19:36:13
【问题描述】:

首先,我是一个早期的初学者程序员,希望得到一些帮助。

我已经编写了以下代码,这些代码是我测试过的:

  • 5 个随机数介于:1 到 39 //num1gen 到 num5gen - 例如 A 组
  • 1 到 14 之间的随机数 //Thunderball - 例如 B 组

我只想按升序计算

我仍然需要将生成的随机数放入如下所示的整数名称中:

  • num1gen = 12
  • num2gen = 24
  • num3gen = 3
  • num4gen = 5
  • num5gen = 32
  • 雷球 = 12

错误:ISO C++ Forbids deceleration of 'i' with no type [-fpremissive] 在第 16 行。

到目前为止,我在以下用户的帮助下完成了代码:

#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;
  }

【问题讨论】:

  • 还有什么问题?
  • 不要在循环中使用srand()
  • 使用vector 存储所有数字,然后对其进行排序。
  • 只需将您的数字读入数组或std::vector 并在输出前对其进行排序。
  • 如果我将这些数字读入一个数组,我仍然可以将生成的随机数放在它们唯一的整数名称中吗?即 num1gen、num2gen 等。仍然持有数字。因为我的程序的其余部分正在检查这些内存名称位置。

标签: c++ random integer srand time.h


【解决方案1】:

如果将 A 中的数字创建为向量,则有一个带有排序的算法标题,如下所示:

#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 &lt;algorithm&gt; 标头中。

【讨论】:

  • 谢谢你,我会试试的!但是,这里会不会把生成的随机数变成num1gen、num2 gen……到num5gen呢?因为为了与我编写的程序的其余部分兼容,我需要将生成的这些数字存储在这些内存位置中。由于它们在整个过程中被多次引用和阅读。
  • @MikeEricson - 不会。您是否根据该要求编辑了原始问题?
  • vector 中没有排序方法。你必须使用void sort (RandomAccessIterator first, RandomAccessIterator last)
  • 我没有编辑我的问题的任何内容,只编辑了代码的缩进。谢谢
  • @TriHard8 将 numA.sort(numA.begin(), numA.end()); 更改为 std::sort(numA.begin(), numA.end());
【解决方案2】:
 #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," "})
}

【讨论】:

  • 我试过这个并收到了这条消息。 #ifndef _CXX0X_WARNING_H #define _CXX0X_WARNING_H 1 #if __cplusplus
  • @Mike Ericson 我使用了 C++11,所以你必须打开 C++11 支持,如消息所示。
  • 很遗憾我找不到那个选项。我正在使用代码块。感谢您的回复。
  • 您不会厌倦在代码中每 2 厘米输入 std:: 吗?
猜你喜欢
  • 2020-01-19
  • 2016-10-08
  • 2023-01-03
  • 1970-01-01
  • 2014-12-15
  • 2017-03-22
  • 1970-01-01
  • 2010-12-10
  • 2021-12-31
相关资源
最近更新 更多