C++ 随机与数组

C++ Random & Array

本文关键字:数组 随机 C++      更新时间:2023-10-16

我尝试将随机函数用于不同的事情。现在它可以工作,但我有一个问题,我想在我的代码中随机生成F,但之后它们需要变成数组。我也需要使用它,如果我需要做它一个一个我认为我的代码会太长和混乱。你知道我该怎么做吗?

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
    srand(time(0));
    //random numbers 1 to 10 for Time:
    int t = rand() % 10 + 1 ;
    cout << "There is "<< t << " Time;
    int* F1 = new int [t];
    int* F2 = new int [t];
    int* F3 = new int [t];
    int* F4 = new int [t];
    int* F5 = new int [t];
    cout << "Time per F: 0 Not available, 1 available;
    //For F1
    for(int i = 0; i < t; i++){
        //random numbers 0 or 1:
        F1[i] = rand() % 2 ;
    }
    cout << "The Time for F1 is ";
    for(int a = 0; a < t; a++){
        cout << " "<< F1[a] <<" ";
    } 
    //For F2
    for(int j = 0; j < t; j++){
        //random numbers 0 or 1:
        F2[j] = rand() % 2 ;
    }
    cout << "The Time slot for F2 is ";
    for(int b = 0; b < t; b++){
        cout << " "<< F2[b] << " ";
    }
    return 0;
}

谢谢

编辑:你给我的解决方案有助于找到解决方案

由于您使用的是c++,并且可以访问标准库,因此可以像std::vector:

那样编写得更干净、更安全。
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    srand(time(0));
    //Random number from 3 to 7
    int numberOfVectors = (rand() % 7) + 3;
    //Random number from 1 to 10.
    int size = (rand() % 10) + 1;
    std::cout << "There are " << numberOfVectors << " vectors." << std::endl;
    std::cout << "Each vector has " << size  << " elements." << std::endl;
    std::vector< std::vector<int> > vectorOfVectorOfInt;
    std::cout << "Value per element: 0 = Not available, 1 = available" << std::endl;
    for(int vec = 0; vec < numberOfVectors; vec++)
    {
        //Create a new vector with 'size' elements.
        std::vector<int> newVector(size);
        for(int i = 0; i < size; i++)
        {
            //Generate a random value between 0 and 50
            newVector[i] = (rand() % 50);
        }
        //Add the vector to our vector-of-vectors.
        vectorOfVectorOfInt.push_back(newVector);
        std::cout << "The values for Vector #" << (vec+1) << " is:";
        for(int b = 0; b < size; b++)
        {
            int value = vectorOfVectorOfInt[vec][b];
            std::cout << "t" << value;
        }
        std::cout << std::endl;
    }
    return 0;
}

您可以运行它并在这里看到结果:http://ideone.com/RWQhjO

使用array:

#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
   srand(time(0));
    //random numbers 1 to 10 for Time:
    int t = rand() % 10 + 1 ;
    cout << "Time per F: 0 Not available, 1 available";
    const int f_num = 5;
    cout << "There is "<< t << "Time";
    int* F = new int*[f_num];
    for(int i = 0; i < f_num; i++){
       F[i] = new int[t];
       cout << "The Time for F"<<i<<" is :";
       for(int j = 0; j < t; j++){
          //random numbers 0 or 1:
          F[i][j] = rand() % 2 ;
          cout << " "<< F1[i][j] <<" ";
       }
    }
}

顺便说一句,你的代码很像c。在c++中,我们通常使用std::vector而不是普通数组,使用http://en.cppreference.com/w/cpp/numeric/random而不是rand()。另外,不要使用using namespace std。这会导致名称冲突。

我尝试对不同的事情使用随机函数。

大概是随机的。

现在它可以工作,但我有一个问题是,我想在我的代码中随机生成F,但它们需要成为数组。

您可以像生成t的值一样生成该值。

const int number_of_fs = rand() % 10 + 1;
std::cout << "Working with " << number_of_fs << " Fs" << std::endl;

我也需要使用它,如果我需要一个接一个地做,我认为我的代码会太长,混乱。你知道我该怎么做吗?

你的代码已经太长太乱,并且有内存泄漏。您应该使用来自标准容器库的容器,而不是指向永远不会得到delete d的指针的指针。

如果你使用std::mapstd::vector,你的代码看起来可能是这样的:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <vector>
int main(){
    srand(time(0));
    //random numbers 1 to 10 for Time:
    const int t = rand() % 10 + 1;
    std::cout << "There is "<< t << " Time" << std::endl;
    const int number_of_fs = rand() % 10 + 1;
    std::cout << "Working with " << number_of_fs << " Fs" << std::endl;
    std::map<int, std::vector<int> > f;
    std::cout << "Time per F: 0 Not available, 1 available" << std::endl;
    for (int f_slot = 1; f_slot <= number_of_fs; ++f_slot) {
        for(int i = 0; i < t; i++){
            //random numbers 0 or 1:
            f[f_slot].push_back(rand() % 2);
        }
        std::cout << "The Time for F" << f_slot << " is " << std::endl;
        std::copy(f[f_slot].begin(), f[f_slot].end(), 
            std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
    }
}

看它运行!

当然,这些容器的内容的填充和打印可能应该提取到单独的函数中,但我不想完全重写您的代码