Like Tree1Likes
  • 1 Post By quzah

Random Generation of unique array.

This is a discussion on Random Generation of unique array. within the C Programming forums, part of the General Programming Boards category; Hello everyone.... I am trying to generate a random unique array which would have in it all the whole numbers ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    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. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    sorry , but i can give it a try with reverse transpose ... me new to C language !

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    iMalc likes this.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating 4 unique random numbers
    By Newklear in forum C Programming
    Replies: 11
    Last Post: 03-08-2011, 04:53 AM
  2. Random Generation
    By davewang in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2010, 02:02 AM
  3. Random but Unique Numbers
    By peckitt99 in forum C++ Programming
    Replies: 12
    Last Post: 11-14-2007, 11:51 AM
  4. Unique random numbers
    By aydin in forum C Programming
    Replies: 7
    Last Post: 11-23-2004, 11:21 AM
  5. Unique Random Numbers in 2D Array
    By kssjbr in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2003, 02:45 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21