10

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?

James
  • 726
  • 2
  • 7
  • 20
  • apply random-number-generator tag – N 1.1 Mar 03 '10 at 10:02
  • See also: http://stackoverflow.com/questions/1218155/random-number-but-dont-repeat, http://stackoverflow.com/questions/1816534/random-playlist-algorithm, http://stackoverflow.com/questions/417831/what-is-the-best-way-of-randomly-re-arranging-a-list-of-items-in-c, http://stackoverflow.com/questions/813935/randomizing-elements-in-an-array – outis Mar 03 '10 at 14:07

10 Answers10

20

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.

sfg
  • 489
  • 4
  • 7
  • 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. – sfg Mar 03 '10 at 11:16
  • 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 Mar 03 '10 at 12:57
  • Just remember that reinventing the wheel also leaves room to reinvent a wide variety of bugs, both obvious and subtle. – Mark B Mar 03 '10 at 15:43
15

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

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
9

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;
}
f4.
  • 3,814
  • 1
  • 23
  • 30
1

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.

Dolbz
  • 2,078
  • 1
  • 16
  • 25
  • "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. – Steve Jessop Mar 03 '10 at 11:38
  • You're right, if I scale to say 1 million elements it'll surely gonna have some performance issues. – James Mar 03 '10 at 13:03
0

I hope it will help:

std::generate(array, array + sizeof(array)/sizeof(int), ::rand);

Below you have full code:

#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<iterator>

int main()
{
    int array[] = {1, 2, 3, 4};
    const unsigned SIZE = sizeof(array)/sizeof(int);

    cout << "Array before: ";
    std::copy(array, array+SIZE, std::ostream_iterator<int>(cout, ", "));

    std::generate(array, array+SIZE, ::rand); // answer for your question

    cout << "\nArray arter:  ";
    std::copy(array, array+SIZE, std::ostream_iterator<int>(cout, ", "));
}

If you want to have smaller numbers than that you can do like that after generating:

std::transform(array, array+SIZE, array, std::bind2nd(std::modulus<int>(), 255));
baziorek
  • 2,502
  • 2
  • 29
  • 43
0

I'd put the 20 random numbers in an array, then copy to another 20 element array, sort one array, and for each number in the sorted array, find the corresponding number in the unsorted array, and put the index of the sort there.

Arthur Kalliokoski
  • 1,627
  • 12
  • 12
0

If you can use containers, I'd just fill a std::set with the numbers from 1 to 20.

Then you can pull a random number from your set and insert it into the array.

Christian Severin
  • 1,793
  • 19
  • 38
0

something like this?

int n = 20; //total element of array
int random = 0, temp=0, start=1;

for(int i=0; i < n; ++i,++start)
{
    array[i] = start;
}

for(int i=0; i<n ; ++i) 
{
    random=rand()%(n-i)+i;
    //swapping, you can make it as a function
    temp = array[i];
    array[i] = array [random];
    array[random] = temp; 

}
infoclogged
  • 3,641
  • 5
  • 32
  • 53
mhd
  • 4,561
  • 10
  • 37
  • 53
0

Other possible way

int array[20]={0};
int values[20];
for(int i = 0; i < 20; i++)
    values[i] = i;
int left = 20;
for(int i = 0; i < 20; i++)
{
    int n = rand() % left;
    array[i] = values[n];
    left--;
    values[n] = values[left];
}

PS filled with nombers from 0 to 19 not from 1 to 20. Same as original

drlazy
  • 71
  • 3
0

Code example for Fisher-Yates:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void shuffle(int array[], size_t size)
{
    if(size) while(--size)
    {
        size_t current = (size_t)(rand() / (1.0 + RAND_MAX) * (size + 1));
        int tmp = array[current];
        array[current] = array[size];
        array[size] = tmp;
    }
}

int main(void)
{
    srand((int)time(NULL));
    rand(); // throw away first value: necessary for some implementations

    int array[] = { 1, 2, 3, 4, 5 };
    shuffle(array, sizeof array / sizeof *array);

    size_t i = 0;
    for(; i < sizeof array / sizeof *array; ++i)
        printf("%i\n", array[i]);

    return 0;
}
Christoph
  • 164,997
  • 36
  • 182
  • 240