Random Number Generator Issues via /r/learnprogramming

Random Number Generator Issues

My task is to write a program that first randomly generates 5,000,000 numbers (integers) that are in the range [0, 5,000,000], using an array or vector to hold the number.

I've written the following number generator that generates numbers between 1 and 25 but I encounter an error when I attempt to do this for 5 million. Could someone point out how I could accomplish this?

#include <iostream> #include <ctime> #include <cstdlib> using namespace std; void printFunc(int[]); void fillFunc(int[]); int main() { int random[5000000]; srand((unsigned)time(NULL)); fillFunc(random); printFunc(random); return 0; } void fillFunc(int arr[]) { for (int i = 1; i < 5000000; i++) { arr[i] = 1 + rand() % 5000000; } } void printFunc(int arr[]) { for (int i = 1; i < 5000000; i++) { cout << arr[i] << endl; } } 

Submitted July 12, 2017 at 09:33PM by nosleeptiltomorrow
via reddit http://ift.tt/2ukMPkX


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s