filling an array with random number

16,423

Solution 1

You could fill the array in sequence and then shuffle it. That would prevent having to ever do more than 20 random number generations.

Fisher-Yates shuffle: can be done in O(n) time.

From wikipedia:

Properly implemented, the Fisher–Yates shuffle is unbiased, so that every permutation is equally likely. The modern version of the algorithm is also rather efficient, requiring only time proportional to the number of items being shuffled and no additional storage space.

Solution 2

Look at std::random_shuffle and std::vector.

Solution 3

you could fill an array with numbers from 1 to 20 and use std::random_shuffle

note you don't need a vector for that matter a simple array will do.
example :

#include <iostream>
#include <algorithm>

using namespace std;

int main( void )
{
        int array[] = { 0, 1, 2, 3, 4 };

        srand( unsigned( time(NULL) ) );

        random_shuffle(array, array+5);

        for(int i=0; i<5; i++)
                cout << array[i] << endl;

        return 0;
}

Solution 4

There's the possibility in your code of a very long running loop. If you're inside the while(!done) loop there's no guarantee you'll ever finish. Obviously with an array of 20 elements this won't in practice be an issue but it could cause problems if you scale this up to many thousands of elements.

A more reliable solution would be to fill the array sequentially and then shuffle it afterwards.

Share:
16,423
James
Author by

James

Updated on June 11, 2022

Comments

  • James
    James almost 2 years

    I'm trying to fill an array of 20 ints with numbers from 1-20 in random sequence. here's my code:

     int lookup[20]={0}; 
     int array[20]={0};
     srand(time(NULL));
     for(int i=0;i<20;++i){ 
        bool done=false;
        while(!done){
          int n=rand()%20;
          if(lookup[n]==0){
              array[i]=n;
              lookup[n]=1;
              done=true;
          }
        }
     }
    

    I've created a lookup array to check if the random number is not yet chosen and stored it in array. As you can see I've created 2 loops, one for traversing array and the while for choosing the random number. In every while loop iteration the number may reappear and causing another while loop. Is there faster way to do this?

  • Philip Potter
    Philip Potter about 14 years
    Yes! Use a standard algorithm; Fisher-Yates is very easy to get wrong.
  • andrewmu
    andrewmu about 14 years
    I've always wondered why it is called random_shuffle. Sort of implies that there is a nonrandom_shuffle.
  • Christoph
    Christoph about 14 years
    don't use rand() % left - see eg members.cox.net/srice1/random/crandom.html
  • pmr
    pmr about 14 years
    +1 for STL. arrays provide RA Iterators as well. No need for std::vector.
  • sfg
    sfg about 14 years
    If your using C++ specifically and not C then graham.reeds suggestion to use std:random_shuffle will get you where you want to go without the peril of making a mistake implementing the shuffle algorithm. If you are using C then there is a code implementation on the Wikipedia page (and likely elsewhere on the web) that you can copy.
  • Steve Jessop
    Steve Jessop about 14 years
    "there's no guarantee you'll ever finish" - the loop terminates with probability 1 if the random number function is good enough, but you're quite right that expected runtime is poor. In the case where the length of the array is greater than RAND_MAX, you obviously have a problem, but that's actually to do with the dodgy way that the code tries to get a random number from 0 .. N-1 with uniform distribution. A Fisher-Yates which does rand()%N would also be weak for large N.
  • James
    James about 14 years
    Yes! I'm using C++ for this. std::random_shuffle is good to go but I think I'm gonna try implementing the shuffle algorithm. Thanks for the reply.
  • James
    James about 14 years
    You're right, if I scale to say 1 million elements it'll surely gonna have some performance issues.
  • andrewmu
    andrewmu about 14 years
    If you are going to use C++ then you might as well take advantage of all that the STL can offer.
  • Mark B
    Mark B about 14 years
    Just remember that reinventing the wheel also leaves room to reinvent a wide variety of bugs, both obvious and subtle.