## How can I create an array of random numbers in C++

Instead of The ELEMENTS being 25 is there a way to randomly generate a large array of elements....10000, 100000, or even 1000000 elements and then use my insertion sort algorithms. I am trying to have a large array of elements and use insertion sort to put them in order and then also in reverse order. Next I used clock() in the time.h file to figure out the run time of each algorithm. I am trying to test with a large amount of numbers.

#define ELEMENTS 25 void insertion_sort(int x[],int length); void insertion_sort_reverse(int x[],int length); int main() { clock_t tStart = clock(); int B[ELEMENTS]={4,2,5,6,1,3,17,14,67,45,32,66,88, 78,69,92,93,21,25,23,71,61,59,60,30}; int x; cout<<"Not Sorted: "<<endl; for(x=0;x<ELEMENTS;x++) cout<<B[x]<<endl; insertion_sort(B,ELEMENTS); cout <<"Sorted Normal: "<<endl; for(x=0;x<ELEMENTS;x++) cout<< B[x] <<endl; insertion_sort_reverse(B,ELEMENTS); cout <<"Sorted Reverse: "<<endl; for(x=0;x<ELEMENTS;x++) cout<< B[x] <<endl; double seconds = clock() / double(CLK_TCK); cout << "This program has been running for " << seconds << " seconds." << endl; system("pause"); return 0; }

**Array of random numbers using C program,** Array of random integers in C I have written the code that shows the 100 random integers in the Create an array of 100 random integers: One mathematical function in C programming that’s relatively easy to grasp is the rand () function. It generates random numbers. Though that may seem silly, it’s the basis for just about every computer game ever invented. Random numbers are a big deal in programming.

Instead of

void insertion_sort(int x[],int length); void insertion_sort_reverse(int x[],int length); int B[ELEMENTS]={4,2,5,6,1,3,17,14,67,45,32,66,88, 78,69,92,93,21,25,23,71,61,59,60,30};

try

void insertion_sort(std::vector<int>& x); void insertion_sort_reverse(std::vector<int>& x); srand(NULL); std::vector<int> B(num_elements); //num_elements can be a variable std::generate(B.begin(), B.end(), rand);

As relates to the task and not the question:
You'll want to run each sort twice in a row, the first without timing, the second with timing.
Your tests aren't fair since one starts from a randomized position, and the other from a sorted position.
You're including IO in your timings, and IO is always slow (`cout`

)
`std::endl`

forces the program to give all the output to the OS immediately, use `'\n'`

.
You're displaying a completely unrelated number of seconds.

int main() { srand(NULL); std::vector<int> B(num_elements); //num_elements can be a variable std::generate(B.begin(), B.end(), rand); std::vector<int> C(B); //make a copy of the data std::cout << "Not Sorted:" << '\n'; for(int i=0;i<B.size();i++) cout<<B[i]<<'\n'; clock_t tStart0 = clock(); insertion_sort(B,ELEMENTS); clock_t tStop0 = clock(); cout <<"Sorted Normal: "<<'\n'; for(int i=0;i<B.size();i++) cout<< B[i] <<'\n'; clock_t tStart1 = clock(); insertion_sort_reverse(C); clock_t tStop1 = clock(); cout <<"Sorted Reverse: "<<'\n'; for(int i=0;i<C.size();i++) cout<< C[i] <<'\n'; double seconds = (tStop1-tStart1 + tStop0-tStart0) / double(CLK_TCK); cout << "This program has been running for " << seconds << " seconds." << endl; system("pause"); return 0; }

**Array of random integers in C - C++ Forum,** C program to generate pseudo-random numbers using rand and random function (Turbo C compiler only). As the random numbers are generated by an As C does not have an inbuilt function for generating a number in the range, but it does have rand function which generate a random number from 0 to RAND_MAX. With the help of rand () a number in range can be generated as num = (rand () % (upper – lower + 1)) + lower

For C++11, I used the time value as initial random seed to generate any number of random data.

See the following sample code

#include <iostream> #include <random> #include <chrono> #include <functional> #include <iterator> constexpr auto MAX = 20; int main(int argc, char* argv[]) { std::default_random_engine re(std::chrono::system_clock::now().time_since_epoch().count()); std::uniform_int_distribution<int> uid(0, MAX); int nums[MAX] {}; std::generate(nums, nums+MAX, std::bind(uid, re)); // apply sort algorithm std::copy(nums, nums+MAX, std::ostream_iterator<int>(std::cout, ",")); std::cout << std::endl; return 0; }

**C program to generate random numbers,** Im trying to get a 20 element array to fill with random numbers from rand. I was told using I tried searching for answers but I can't make it work. C program to generate random numbers C program to generate pseudo-random numbers using rand and random function (Turbo C compiler only). As the random numbers are generated by an algorithm used in a function they are pseudo-random, this is the reason that word pseudo is used. Function rand () returns a pseudo-random number between 0 and RAND_MAX.

**Filling an array with random numbers.,** How to generate a random number in a given range in C. Examples: Input : Lower = 50, Upper = 100, Count of random Number = 5 Output : 91 34 21 88 29 int Min = 0; int Max = 20; // this declares an integer array with 5 elements // and initializes all of them to their default value // which is zero int[] test2 = new int[5]; Random randNum = new Random(); for (int i = 0; i < test2.Length; i++) { test2[i] = randNum.Next(Min, Max); } alternatively you could use LINQ:

**Generating random number in a range in C,** In a real random number generator, the seed should initialize as a random value, such as the system time. Here, the rand function will produce the same values Access the array element from that random index and you will get your number, since all your numbers in the array seems to be distinct. int[] numbers = new int[5] { 32, 67, 88, 13, 50 }; Random rd = new Random(); int randomIndex = rd.Next(0, 5); int randomNumber = numbers[randomIndex];

**Arrays,** A C++ program to search the user's number in an array containing 100 random numbers between "1" and "1000" is shown below: Program: //Declare the header parameters : array of integers number of integers to store in the array description: generate "number" random numbers from 1 to 100 and store them in positions [0..number-1] So the array is always the same size, but may be filled with a different number of random values on each run void printArray (int array[], int number) parameters : array of