Unique Random Numbers

Discussion in 'C++' started by Xoomer, Mar 25, 2007.

  1. Xoomer

    Xoomer Guest

    Hi,

    Can some post a code with simple Unique Random Numbers.

    Random numbers stored in arr[20] but unique.
     
    Xoomer, Mar 25, 2007
    #1
    1. Advertising

  2. On 2007-03-25 16:31, Xoomer wrote:
    > Hi,
    >
    > Can some post a code with simple Unique Random Numbers.
    >
    > Random numbers stored in arr[20] but unique.


    From the top of my head:

    #include <cstdlib>
    #include <ctime>

    int main()
    {
    srand(time(0));
    int arr[20];

    for (int i = 0; i < 20; ++i)
    arr = rand();

    return 0;
    }

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Mar 25, 2007
    #2
    1. Advertising

  3. Xoomer

    Xoomer Guest

    On Mar 25, 3:53 pm, Erik Wikström <> wrote:
    > On 2007-03-25 16:31, Xoomer wrote:
    >
    > > Hi,

    >
    > > Can some post a code with simpleUniqueRandomNumbers.

    >
    > >Randomnumbersstored in arr[20] butunique.

    >
    > From the top of my head:
    >
    > #include <cstdlib>
    > #include <ctime>
    >
    > int main()
    > {
    > srand(time(0));
    > int arr[20];
    >
    > for (int i = 0; i < 20; ++i)
    > arr = rand();
    >
    > return 0;
    >
    > }
    >
    > --
    > Erik Wikström


    Thanks. But the number may repeat the same.. its not Unique.
     
    Xoomer, Mar 25, 2007
    #3
  4. Xoomer

    Xoomer Guest

    On Mar 25, 3:53 pm, Erik Wikström <> wrote:
    > On 2007-03-25 16:31, Xoomer wrote:
    >
    > > Hi,

    >
    > > Can some post a code with simpleUniqueRandomNumbers.

    >
    > >Randomnumbersstored in arr[20] butunique.

    >
    > From the top of my head:
    >
    > #include <cstdlib>
    > #include <ctime>
    >
    > int main()
    > {
    > srand(time(0));
    > int arr[20];
    >
    > for (int i = 0; i < 20; ++i)
    > arr = rand();
    >
    > return 0;
    >
    > }
    >
    > --
    > Erik Wikström


    Thanks. But the random number may repeat the same.. its not Unique.
     
    Xoomer, Mar 25, 2007
    #4
  5. Xoomer

    red floyd Guest

    red floyd, Mar 25, 2007
    #5
  6. Xoomer

    osmium Guest

    "Xoomer" writes:

    > Can some post a code with simple Unique Random Numbers.
    >
    > Random numbers stored in arr[20] but unique.


    The magic word is shuffle. See what you can find on the net.
     
    osmium, Mar 25, 2007
    #6
  7. Xoomer wrote:
    > Hi,
    >
    > Can some post a code with simple Unique Random Numbers.
    >
    > Random numbers stored in arr[20] but unique.


    Well, if the array is only 20 elements long, why not
    generate a random number for every entry i, and then
    compare it with all previous numbers arr[0..i-1], if
    it compares, throw it away and generate a new one?

    Not very efficient, but for 20 elements it would be fine.

    HTH,
    - J.
     
    Jacek Dziedzic, Mar 25, 2007
    #7
  8. Xoomer

    rossum Guest

    On 25 Mar 2007 07:59:37 -0700, "Xoomer" <> wrote:

    >On Mar 25, 3:53 pm, Erik Wikström <> wrote:
    >> On 2007-03-25 16:31, Xoomer wrote:
    >>
    >> > Hi,

    >>
    >> > Can some post a code with simpleUniqueRandomNumbers.

    >>
    >> >Randomnumbersstored in arr[20] butunique.

    >>
    >> From the top of my head:
    >>
    >> #include <cstdlib>
    >> #include <ctime>
    >>
    >> int main()
    >> {
    >> srand(time(0));
    >> int arr[20];
    >>
    >> for (int i = 0; i < 20; ++i)
    >> arr = rand();
    >>
    >> return 0;
    >>
    >> }
    >>
    >> --
    >> Erik Wikström

    >
    >Thanks. But the number may repeat the same.. its not Unique.

    Highly unlikely. The repeat length of any halfway decent RNG is a lot
    longer than 20. Knuth Chapter Three refers.

    rossum
     
    rossum, Mar 25, 2007
    #8
  9. Xoomer

    Pete Becker Guest

    rossum wrote:
    > Highly unlikely. The repeat length of any halfway decent RNG is a lot
    > longer than 20. Knuth Chapter Three refers.
    >


    That's repetition of the sequence, not of individual values.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Mar 25, 2007
    #9
  10. Xoomer

    Jim Langston Guest

    "Xoomer" <> wrote in message
    news:...
    > Hi,
    >
    > Can some post a code with simple Unique Random Numbers.
    >
    > Random numbers stored in arr[20] but unique.


    Unique, but sorted.

    #include <iostream>
    #include <string>
    #include <set>
    #include <ctime>

    int main()
    {
    std::set<int> RandomNumbers;
    std::srand( std::clock() );

    while ( RandomNumbers.size() < 20 )
    RandomNumbers.insert( RandomNumbers.end(), rand() );

    for ( std::set<int>::iterator it = RandomNumbers.begin(); it !=
    RandomNumbers.end(); ++it )
    std::cout << *it << " ";
    std::cout << std::endl;

    std::string wait;
    std::getline( std::cin, wait );

    }
     
    Jim Langston, Mar 26, 2007
    #10
  11. How about just make the array first and then mixing the numbers by
    using the random function?

    For example...

    #include <cstdlib>
    #include <ctime>

    int main()
    {
    srand(time(0));
    int arr[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
    int repeat = rand() % HOW_MANY_TIMES_DO_YOU_WANT;
    int pos = 0;

    for (int i = 0; i < repeat; ++i)
    {
    pos = rand() % 20;

    if(pos == 19)
    swap(0, 19); // Swapping the first and the last
    else
    swap(pos, pos+1); // Swapping with the next
    }
    return 0;
    }

    I just made the source code without thinking deeply and using IDE so
    if there is a silly mistake then please forgive me =)

    Cheers,

    On Mar 26, 10:29 am, "Jim Langston" <> wrote:
    > "Xoomer" <> wrote in message
    >
    > news:...
    >
    > > Hi,

    >
    > > Can some post a code with simple Unique Random Numbers.

    >
    > > Random numbers stored in arr[20] but unique.

    >
    > Unique, but sorted.
    >
    > #include <iostream>
    > #include <string>
    > #include <set>
    > #include <ctime>
    >
    > int main()
    > {
    > std::set<int> RandomNumbers;
    > std::srand( std::clock() );
    >
    > while ( RandomNumbers.size() < 20 )
    > RandomNumbers.insert( RandomNumbers.end(), rand() );
    >
    > for ( std::set<int>::iterator it = RandomNumbers.begin(); it !=
    > RandomNumbers.end(); ++it )
    > std::cout << *it << " ";
    > std::cout << std::endl;
    >
    > std::string wait;
    > std::getline( std::cin, wait );
    >
    > }
     
    Alexander D. B. Kim, Mar 26, 2007
    #11
  12. Xoomer

    Pete Becker Guest

    Alexander D. B. Kim wrote:
    > How about just make the array first and then mixing the numbers by
    > using the random function?
    >


    Use std::random_shuffle.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Mar 26, 2007
    #12
  13. Xoomer wrote:
    > On Mar 25, 3:53 pm, Erik Wikström <> wrote:
    >> On 2007-03-25 16:31, Xoomer wrote:
    >>
    >>> Hi,
    >>> Can some post a code with simpleUniqueRandomNumbers.
    >>> Randomnumbersstored in arr[20] butunique.

    >> From the top of my head:
    >>
    >> #include <cstdlib>
    >> #include <ctime>
    >>
    >> int main()
    >> {
    >> srand(time(0));
    >> int arr[20];
    >>
    >> for (int i = 0; i < 20; ++i)
    >> arr = rand();
    >>
    >> return 0;
    >>
    >> }
    >>
    >> --
    >> Erik Wikström

    >
    > Thanks. But the random number may repeat the same.. its not Unique.
    >

    Yes, this might generate repeated random numbers.
    You can try generating the index randomly and assign
    monotonically increasing count value to that array element,
    after checking the presence of a flag.

    Here's how:

    int main()
    {
    srand(time(0));
    int arr[20] = {-1}; //init the array with a flag

    for (int i = 0; i < 20; ++i)
    {
    if(arr == -1) //assign counter only if flag is found
    {
    arr[rand()%20] = i;
    }
    }
    return 0;

    }

    Hope this helps...

    regards,
    Seemanta
     
    Seemanta Dutta, Mar 26, 2007
    #13
  14. Xoomer

    rossum Guest

    On Sun, 25 Mar 2007 16:14:23 -0400, Pete Becker
    <> wrote:

    >rossum wrote:
    >> Highly unlikely. The repeat length of any halfway decent RNG is a lot
    >> longer than 20. Knuth Chapter Three refers.
    >>

    >
    >That's repetition of the sequence, not of individual values.

    Correct, but with a reasonable linear congruential generator
    individual values do not repeat until the whole sequence repeats. If
    the OP limits responses to a given range then repeats do become
    possible.

    rossum
     
    rossum, Mar 26, 2007
    #14
  15. Thanks =)

    On Mar 26, 9:47 pm, Pete Becker <> wrote:
    > Alexander D. B. Kim wrote:
    >
    > > How about just make the array first and then mixing the numbers by
    > > using the random function?

    >
    > Use std::random_shuffle.
    >
    > --
    >
    > -- Pete
    > Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    > Author of "The Standard C++ Library Extensions: a Tutorial and
    > Reference." (www.petebecker.com/tr1book)
     
    Alexander D. B. Kim, Mar 26, 2007
    #15
  16. Xoomer

    Guest

    On Mar 25, 7:31 am, "Xoomer" <> wrote:
    > Hi,
    >
    > Can some post a code with simple Unique Random Numbers.
    >
    > Random numbers stored in arr[20] but unique.


    Jon Bentley's book Programming Pearls Second Edition has this exact
    question and discussion. Check chapter 1 problem 4.
     
    , Mar 26, 2007
    #16
  17. Xoomer

    Kai-Uwe Bux Guest

    rossum wrote:

    > On Sun, 25 Mar 2007 16:14:23 -0400, Pete Becker
    > <> wrote:
    >
    >>rossum wrote:
    >>> Highly unlikely. The repeat length of any halfway decent RNG is a lot
    >>> longer than 20. Knuth Chapter Three refers.
    >>>

    >>
    >>That's repetition of the sequence, not of individual values.

    > Correct, but with a reasonable linear congruential generator
    > individual values do not repeat until the whole sequence repeats.


    That's one of their weaknesses: the Birthday paradox shows that repetitions
    _should_ happen much earlier.

    > If
    > the OP limits responses to a given range then repeats do become
    > possible.


    They also become possible if the OP uses a different RNG.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 26, 2007
    #17
  18. On Mar 26, 9:53 pm, Seemanta Dutta <> wrote:
    > Xoomer wrote:
    > > On Mar 25, 3:53 pm, Erik Wikström <> wrote:
    > >> On 2007-03-25 16:31, Xoomer wrote:

    >
    > >>> Hi,
    > >>> Can some post a code with simpleUniqueRandomNumbers.
    > >>> Randomnumbersstored in arr[20] butunique.
    > >> From the top of my head:

    >
    > >> #include <cstdlib>
    > >> #include <ctime>

    >
    > >> int main()
    > >> {
    > >> srand(time(0));
    > >> int arr[20];

    >
    > >> for (int i = 0; i < 20; ++i)
    > >> arr = rand();

    >
    > >> return 0;

    >
    > >> }

    >
    > >> --
    > >> Erik Wikström

    >
    > > Thanks. But the random number may repeat the same.. its not Unique.

    >
    > Yes, this might generate repeated random numbers.
    > You can try generating the index randomly and assign
    > monotonically increasing count value to that array element,
    > after checking the presence of a flag.
    >
    > Here's how:
    >
    > int main()
    > {
    > srand(time(0));
    > int arr[20] = {-1}; //init the array with a flag
    >
    > for (int i = 0; i < 20; ++i)
    > {
    > if(arr == -1) //assign counter only if flag is found
    > {
    > arr[rand()%20] = i;
    > }
    > }
    > return 0;
    >
    > }
    >
    > Hope this helps...
    >
    > regards,
    > Seemanta


    what if "rand()%20" keeps generating the same number (what a
    coincidence!!!) =P
     
    Alexander D. B. Kim, Apr 11, 2007
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lallous
    Replies:
    5
    Views:
    576
    lallous
    Oct 20, 2003
  2. globalrev
    Replies:
    4
    Views:
    771
    Gabriel Genellina
    Apr 20, 2008
  3. Jimmy Palmer

    generating unique random numbers

    Jimmy Palmer, Mar 25, 2008, in forum: Ruby
    Replies:
    14
    Views:
    216
    lasitha
    Mar 27, 2008
  4. alanbe
    Replies:
    15
    Views:
    191
    Michael Winter
    Jun 3, 2005
  5. VK
    Replies:
    15
    Views:
    1,174
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page