I'm trying to switch to <random> instead of using the older rand(). After googling some examples I came up with function to generate an array of random ints between 1 and 10, but there's 2 problems.
- When I generate an array of size 20, some elements always have the same value with repeated experiments.
- The values far exceed 10 (by more than a million sometimes), and I also get zeros, but so far no negatives observed.
without further ado:
Code:
template<size_t N>
std::array<int,N> random_data(){
std::array<int,N> arr; // I get only zeros when I brace initialize this
//std::random_device rd;
//std::mt19937 rg(rd());
std::default_random_engine rg;
std::uniform_int_distribution<> dist(1, 10);
for (int x : arr ){x = dist(rg);}
return arr;
}
//the code in main:
auto mydata = random_data<20>();
for (auto x : mydata) cout<<x<<"\n";
Anyone see what I'm doing wrong?