This program is to take an array of random numbers ranging from 10, 100, 1000 elements. The program is behaving correctly with 10 elements. When I input 100, and 1000 elements it will only run the program when I paste them at the first cin, "cin >> elements". If I input the numbers in the second cin, "cin >> arr[i]" it never executes the program. But it will not process 10 elements when I input on the first cin, "cin >> elements". I have no idea what is going on... Any help would be greatly appreciated.
Your question makes no sense.
You say "random numbers" but there are no random numbers being generated.
You talk about the "first cin" and the "second cin", but there is only one cin in the program.
And with the program you've shown there's no reason why the number of inputs (10, 100, 1000) should make a difference.
Yes I type 10, 100, or 1000.
So if I type 1000, then paste the 1000 elements it never executes the remaining part of the program. It just sits with the 1000 elements.
But... if I paste those 1000 elements in the first cin, it will execute the entire program. It makes no sense!!
#include <iostream>
#include <random>
#include <chrono>
#include <vector>
int main()
{
// a time-based seed value using the system clock
unsigned seed1 { static_cast<unsigned> ( std::chrono::system_clock::now().time_since_epoch().count() ) };
// a seed value using the non-deterministic random number generator
unsigned seed2 { static_cast<unsigned> ( std::random_device{}() )};
// creating a random number engine, seeding it on creation
// there are other engines and adaptors available
std::default_random_engine rng(seed1);
// seed/reseed after construction
rng.seed(seed2);
// create a distribution, arbitrarily chosen to be between 1 and 100 inclusive
std::uniform_int_distribution<int> dis(1, 100);
std::cout << "How many elements? ";
int num_elements { };
std::cin >> num_elements;
// let's create a sized vector to hold the number of elements
// default constructed to be zero-filled
std::vector<int> ran_numbers(num_elements);
for (size_t loop { }; loop < num_elements; loop++)
{
// fill each elements with a random number
// no need for push_back
ran_numbers[loop] = dis(rng);
}
int count { 0 };
// loop through the vector using a range based for loop
for (constauto& itr : ran_numbers)
{
std::cout << itr << '\t';
count++;
if (count % 10 == 0) { std::cout << '\n'; }
}
std::cout << '\n';
}
If you need the same series of "random" numbers each time your program runs use the same numeric value for your seed.
@Rambus Jarbus
This is how you do it using Xcode on a Mac, the terminal and random.org
I used the random integer generator. It's the hidden return that's causing the apparent problem.
1. Go to random.org select the number of random numbers you want.
2. Make the no. of columns the same as the no. of numbers. ie get a single row of random numbers.
3. Copy the row of numbers.
3. Run the executable in Terminal
4. Specify the number of numbers, press return.
5. At the prompt for the elements simply paste the (single) row of numbers.
6. The rest follows.
Note: This works for 10 and 100 and I have no doubt 1000 will too.