|
| ||||||||||
| Tags: c language, class, file handle, function, main, object |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to fill an array with random number in c++?
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. |
|
#2
| ||||
| ||||
| 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;
}
}
} |
|
#3
| ||||
| ||||
| 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;
}
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| ||||
| ||||
| 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];
} |
|
#5
| ||||
| ||||
| 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;
} |
|
#6
| ||||
| ||||
| 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;
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to fill an array with random number in c++?" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Number of HDDs recommended for RAID 5 array | servan$ | Hardware Peripherals | 4 | 10-02-2012 02:31 PM |
| Generate some quality of random number via Random Number Generation function | aMADeO! | Windows Software | 4 | 13-01-2012 04:07 PM |
| How to use "Math.random()" to generate a random number in JavaScript? | Silent~Kid | Software Development | 5 | 03-02-2010 04:06 AM |
| Want an array of Random numbers | InterNetWorKed | Software Development | 4 | 03-11-2009 01:17 PM |
| Fill an array using a mysql database | Jacek | Software Development | 3 | 16-04-2009 12:12 AM |