Thread: Random Generation of unique array.

1. Random Generation of unique array.

Hello everyone....

I am trying to generate a random unique array which would have in it all the whole numbers up to n.
For an example.
If n=10
Then it may give the following output
9
5
4
2
3
1
0
8
6
7

Can it be done without using the random function in loops?

2. sorry , but i can give it a try with reverse transpose ... me new to C language !

3. There are two general approaches:
1. Loop over the elements of the array and generate a random number, within the given range, for each of them. Before moving on to the next element, check that the number generated is not a duplicate. If it is, generate a random number again, repeating the check until the number generated is not a duplicate.
2. Populate the array with numbers from the given range (in this case: numbers from 0 to n in say, ascending order). Shuffle array.

It sounds like option #2 is likely to be good here.

4. 3. Pop and squish.
Code:
```#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define O0 10
#define OO(oo) for( oo = 0x0; oo < O0; oo++ )
int Ox0( int OX0[], int oO )
{
int ox0 = OX0[ 0x0 ];
if( oO > 0x0 )
{
int o0 = 0x0, x0 = rand() % oO;
ox0 = OX0[ x0 ];

for( o0 = x0 + 1; o0 < oO; o0++ )
OX0[ o0 - 1 ] = OX0[ o0 ];
}
return ox0;
}

int main( void )
{
int oO[ O0 ] = { 0x0 }, Oo[ O0 ] = { 0x0 }, oo = 0x0;

srand( time( NULL ) );

OO( oo ) Oo[ oo ] = oo;
OO( oo ) oO[ oo ] = Ox0( Oo, O0 - oo );
OO( oo ) printf( "%d\n", oO[ oo ] );

return 0x0;
}```

Quzah.