Fill a vector with random numbers c++

后端 未结 8 1243
难免孤独
难免孤独 2020-12-14 17:55

I\'ve got a vector that I\'m trying to fill up with random numbers. I keep running into an issue however that the vector mostly outputs 0 each time that I\'m running it (it

相关标签:
8条回答
  • 2020-12-14 18:05

    You are calling the wrong index in your vector

    cout << myVector[i] << endl;
    
    0 讨论(0)
  • 2020-12-14 18:11

    Just adding my 2 cents... This response is similar to the one given by Marko Tunjic, but it doesn't use std::rand from C, but C++11 features instead. It allows you to use the distribution of your choice, uniform_int_distribution in the example below.

    #include <algorithm>
    #include <iostream>
    #include <limits>
    #include <random>
    #include <vector>
    
    static std::vector<int> generate_data(size_t size)
    {
        using value_type = int;
        // We use static in order to instantiate the random engine
        // and the distribution once only.
        // It may provoke some thread-safety issues.
        static std::uniform_int_distribution<value_type> distribution(
            std::numeric_limits<value_type>::min(),
            std::numeric_limits<value_type>::max());
        static std::default_random_engine generator;
    
        std::vector<value_type> data(size);
        std::generate(data.begin(), data.end(), []() { return distribution(generator); });
        return data;
    }
    
    int main()
    {
        for (auto i = 0u; i < 5; ++i)
        {
            std::vector<int> myVector = generate_data(10);
            myVector = generate_data(10);
    
            std::cout << "myVector (iteration " << i << "): ";
            for (auto v: myVector)
            {
                std::cout << v << ",";
            }
            std::cout << "\n";
        }
    }
    
    0 讨论(0)
  • 2020-12-14 18:15
    // here I generate a vector that contains random vectors
    // for example, after this code, vec = { {1,4,8}, {1,3,6,7,9}, {2,5,6} }
    
    #include <vector>
    #include <time.h>
    
    void generate_random_vectors(const int num_of_rand_vectors, vector<vector<int>> &vec) {
        for (int j = 0; j < num_of_rand_vectors; ++j) {
            // the vector size will be randomal: between 0 to 19
            int vec_size = (rand() % 20);
            vector<int> rand_vec(vec_size);
            for (int k = 0; k < vec_size; ++k) {
                // each vec element will be randomal: between 1 to 20
                rand_vec[k] = 1 + (rand() % 20);
            }
            // each vector will be sorted if you want to
            sort(rand_vec.begin(), rand_vec.end());
            // push each of the 'num_of_rand_vectors'-th vectors into vec
            vec.push_back(rand_vec);
        }
    }
    
    void main() {
    
        srand(static_cast<unsigned int>(time(NULL)));
    
        // input vector containing random sorted vectors
        vector<vector<int>> vec;
        generate_random_vectors(3, vec);
    }
    
    0 讨论(0)
  • 2020-12-14 18:15
    cout << myVector[b] ?!
    

    it should be : cout << myVector[i];

    0 讨论(0)
  • 2020-12-14 18:18

    What about simply:

    #include <vector>
    #include <algorithm>
    #include <ctime>
    
    std::srand(unsigned(std::time(nullptr)));
    std::vector<int> v(1000);
    std::generate(v.begin(), v.end(), std::rand);
    
    0 讨论(0)
  • 2020-12-14 18:27

    You can use std::generate algorithm to fill a vector of n elements with random numbers.

    In modern C++ it’s recommended not to use any time-based seeds and std::rand, but instead to use random_device to generate a seed. For software-based engine, you always need to specify the engine and distribution. Read More..

    #include <random>
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    #include <functional>
    
    using namespace std;
    
    int main()
    {
        // First create an instance of an engine.
        random_device rnd_device;
        // Specify the engine and distribution.
        mt19937 mersenne_engine {rnd_device()};  // Generates random integers
        uniform_int_distribution<int> dist {1, 52};
    
        auto gen = [&dist, &mersenne_engine](){
                       return dist(mersenne_engine);
                   };
    
        vector<int> vec(10);
        generate(begin(vec), end(vec), gen);
    
        // Optional
        for (auto i : vec) {
            cout << i << " ";
        }
    
    
    }
    

    If you want to rearrange the elements of a range in a random order:

      std::shuffle(begin(vec), end(vec), mersenne_engine);
    
    0 讨论(0)
提交回复
热议问题