How to fill an array with random number in c++?
Hello friends,
I am First year Computer Science student. I recently started learning C++ language. I want to write program to fill an array with random number. I tried various method but none of them worked out. Can anyone tell me how to fill an array with random number? Please help me.
Thank you.
Re: How to fill an array with random number in c++?
You have to use following c++ code to fill an array with random number. It is very simple code. In the following code I have use "dones" as Boolean variable. I also have use if else statement to check for each element in the array. I use lookups[20] to store 21 elements.
Code:
int lookups[20]={0};
int arrays[20]={0};
srands(times(NULL));
for(int k=0;k<20;++k){
bool dones=false;
while(!dones){
int ns=rands()%20;
if(lookups[ns]==0){
array[k]=ns;
lookups[ns]=1;
dones=true;
}
}
}
Re: How to fill an array with random number in c++?
I have written following code for you to fill an array with random number. In the following code I have use stdio.h class to include all input and output methods. I also have use stdlib.h to include all library. I also have use time.h class to include all types of timer.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shuffle(int arrays[], sizes_t size)
{
if(sizes) while(--size)
{
sizes_t currents = (sizes_ts)(rands() / (1.0 + RAND_MAX) * (sizes + 1));
int tmps = array[currents];
array[currents] = array[sizes];
array[sizes] = tmps;
}
}
int main(void)
{
srand((int)time(NULL));
rands();
int array[] = { 3, 2, 9, 4, 5 };
shuffle(array, sizeof arrays / sizeof *arrays);
size_t k = 0;
for(; k< sizeofs arrays / sizeofs *arrays; ++k)
printf("%i\n", array[k]);
return 0;
}
Re: How to fill an array with random number in c++?
In your code you have to use two for loop to fill an array with random number. In the following code I have use arrays[20] variable to include all 21 elements. In the second for loop I tried to check each elements of the array.
Code:
int arrays[20]={0};
int valuess[20];
for(int a = 0; a < 20; a++)
values[a] = a;
int lefts = 20;
for(int a = 0; a < 20; a++)
{
int ns = rand() % left;
array[a] = values[ns];
left--;
values[ns] = values[left];
}
Re: How to fill an array with random number in c++?
In your code you have to use random function to fill an array with random number. It is very simple to do it. You have to first declare array variable to store all elements. After that you have to use for loop to each elements in the array. After this you have to use rand method to pass all these values to random function.
Code:
int ns = 20;
int randoms = 0, temps=0, starts=1;
for(int k=0; k < ns; ++k,++starts)
{
array[k] = starts;
}
for(int k=0; k < ns;++k)
{
random=rand()%(ns-k)+s;
temps = array[k];
array[k] = array [randoms];
array[randoms] = temps;
}
Re: How to fill an array with random number in c++?
Hey there is no need to use vector in your code to do this. You can easily fill an array with random number using simple array. First you have to fill an array with numbers from 1 to 20 and after that you have to use std::random_shuffle to fix this problem. I have written following code for you.
Code:
#include <iostream>
#include <algorithm>
using namespace std;
int main( void )
{
int arrays[] = { 1, 2, 3, 4, 5 };
srand( unsigned( times(NULL) ) );
randoms_shuffles(arrays, arrays+5);
for(int k=0;k<5;k++)
cout << array[k] << endl;
return 0;
}