To generate a random numbers

Discussion in 'C++' started by Anamika, Aug 12, 2006.

  1. Anamika

    Anamika Guest

    I am doing a project where I want to generate random numbers for say n
    people....
    These numbers should be unique for n people.

    Two people should not have same numbers....

    Also the numbers should not be repeted..

    Do anyone have some nice algorithm for that? Or anyone can suggest me
    any type of book or site for that purpose?

    Thaks for answer....
    Anamika, Aug 12, 2006
    #1
    1. Advertising

  2. Anamika

    John Carson Guest

    "Anamika" <> wrote in message
    news:
    > I am doing a project where I want to generate random numbers for say n
    > people....
    > These numbers should be unique for n people.
    >
    > Two people should not have same numbers....
    >
    > Also the numbers should not be repeted..
    >
    > Do anyone have some nice algorithm for that? Or anyone can suggest me
    > any type of book or site for that purpose?
    >
    > Thaks for answer....


    Just create an array with non-random numbers and then call the
    random_shuffle function from the C++ standard library (declared in the
    <algorithm> header).

    --
    John Carson
    John Carson, Aug 12, 2006
    #2
    1. Advertising

  3. Anamika

    Jim Langston Guest

    "Anamika" <> wrote in message
    news:...
    >I am doing a project where I want to generate random numbers for say n
    > people....
    > These numbers should be unique for n people.
    >
    > Two people should not have same numbers....
    >
    > Also the numbers should not be repeted..
    >
    > Do anyone have some nice algorithm for that? Or anyone can suggest me
    > any type of book or site for that purpose?
    >
    > Thaks for answer....


    It depends on your range of random numbers. If it's rather arbitrary
    (0-65535 or something) then I think the simplest way would to create a
    random number using rand, then look and see if it's been used already. If
    it has, then generate another one. For small values of n this shouldn't be
    a problem. If you're trying to generate a random number for everyone in the
    united states however it might be a bit time consuming.
    Jim Langston, Aug 12, 2006
    #3
  4. > Do anyone have some nice algorithm for that? Or anyone can suggest me
    > any type of book or site for that purpose?


    Using the built-in C/C++ facilities for random number generation is
    almost certainly a terrible idea. The rand() method is totally
    inadequate for a large number of users and purposes.

    I'd suggest using your operating system's built-in facilities for
    random number generation. On Windows, check MSDN
    (http://msdn.microsoft.com) for CryptAcquireContext and CryptGenRandom.
    On UNIX, check to see if /dev/urandom will meet your needs.

    This is an area that is far, far outside the realm of the standard C++
    language. I'd suggest that you try reposting this message in a
    newsgroup appropriate for your operating system; they'll be able to
    give you much more help than we can.
    Robert J. Hansen, Aug 13, 2006
    #4
  5. Anamika

    Cristiano Guest

    Anamika wrote:
    > I am doing a project where I want to generate random numbers for say n
    > people....
    > These numbers should be unique for n people.
    >
    > Two people should not have same numbers....
    >
    > Also the numbers should not be repeted..


    y =y * 2147001325L + 715136305L;

    initialize the y (which, obviously, is a 32-bit unsigned long) with any
    number in range 0 to 2^32-1, then it generates 2^32 unique numbers from 0 to
    0xffffffff.
    After 2^32 numbers are generated, the function will repeat exactly the same
    sequence.

    Very important: the numbers are *not* random, but just pseudo-random.

    Cristiano
    Cristiano, Aug 13, 2006
    #5
  6. Anamika

    Kevin Handy Guest

    Anamika wrote:
    > I am doing a project where I want to generate random numbers for say n
    > people....
    > These numbers should be unique for n people.
    >
    > Two people should not have same numbers....
    >
    > Also the numbers should not be repeted..
    >
    > Do anyone have some nice algorithm for that? Or anyone can suggest me
    > any type of book or site for that purpose?
    >
    > Thaks for answer....
    >


    Set a counter (n = 1)

    To assign a "random" number

    person = n++;

    The randomness facter will be low. You can hide the
    fact somewhat by swapping bits in the generated number.

    x = (n & 1 << 3) | (n & 2 >> 1) | (n & 3 >> 1) <etc...>

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Kevin Handy, Aug 14, 2006
    #6
  7. Anamika

    Jim Langston Guest

    "Anamika" <> wrote in message
    news:...
    >I am doing a project where I want to generate random numbers for say n
    > people....
    > These numbers should be unique for n people.
    >
    > Two people should not have same numbers....
    >
    > Also the numbers should not be repeted..
    >
    > Do anyone have some nice algorithm for that? Or anyone can suggest me
    > any type of book or site for that purpose?
    >
    > Thaks for answer....


    I just though of another way. Use std::set.

    (pseudo code)

    std::set<int> MyRandoms;
    while (MyRandoms.size() < n )
    MyRandoms.insert( randomfunction( MaxValue ) );
    Jim Langston, Aug 15, 2006
    #7
    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. Levi Campbell
    Replies:
    20
    Views:
    722
    Levi Campbell
    Nov 28, 2005
  2. Anthony Liu
    Replies:
    14
    Views:
    1,171
    Terry Reedy
    Apr 24, 2006
  3. Anthony Liu
    Replies:
    1
    Views:
    457
    Paul Rubin
    Apr 30, 2006
  4. globalrev
    Replies:
    4
    Views:
    745
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,126
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page