In: Computer Science
c++
give ways of filling array of uniform random numbers
1) Array of random numbers using C++ program,
Create an array:
int my_array[100];. Seed the random number generator srand(0);.
Loop over your array and fill it up!:
int i;
for (i = 0; i < 100; i++)
try to get a 20 element array to fill with random numbers from rand. I was told using % 99 is 100 since the count starts at 0. Test and using brackets and +1 with my array increments the array to the next value and doesn't add 1.
2) Fill array with random numbers, Simple programming task - fill array with random integer values. We need to include time. h to Duration: C++ library has a function that generates random numbers, called the rand() function.
code:
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
srand(time(NULL));
const unsigned int sizeOfArray =10;
int numberArray[sizeOfArray];
for(int i=0;i<sizeOfArray;i++)
{
numberArray[i]=rand()%50;
cout<<numberArray[i]<<endl;
}
numberArray[0]=500;
numberArray[9]=1000;
for(int i=0; i<sizeOfArray ; i++)
{
cout<<numberArray[i]<<endl;
}
system("PAUSE");
return 0;
}
output:
13
42
5
22
46
36
22
10
46
35
500
42
5
22
46
36
22
10
46
1000
code(SS):
output(SS):
This is ways of filling array of uniform random numbers.