Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to fill an array with random number in c++?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-03-2010
Member
 
Join Date: Nov 2009
Posts: 67
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.

Reply With Quote
  #2  
Old 03-03-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
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;
      }
    }
 }
Reply With Quote
  #3  
Old 03-03-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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;
}
Reply With Quote
  #4  
Old 03-03-2010
Member
 
Join Date: Feb 2008
Posts: 1,845
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];
}
Reply With Quote
  #5  
Old 03-03-2010
Member
 
Join Date: Jan 2008
Posts: 1,514
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; 

   }
Reply With Quote
  #6  
Old 03-03-2010
Member
 
Join Date: Apr 2008
Posts: 1,947
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;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 08:13 PM.