generate rand number

Discussion in 'C++' started by Larry, Jan 26, 2010.

  1. Larry

    Larry Guest

    Hi,

    I need to create a tiny function to generate a random number ranging
    from: 1000000 to 9999999. Also, the number should be unique even if I call
    the function 100 times in a second...can it actually be done?

    All I know for the moment is this: srand(time(0)): rand(); but it is not
    that great :-(

    thanks
     
    Larry, Jan 26, 2010
    #1
    1. Advertising

  2. Larry

    osmium Guest

    "Larry" wrote:

    > I need to create a tiny function to generate a random number ranging
    > from: 1000000 to 9999999. Also, the number should be unique even if I call
    > the function 100 times in a second...can it actually be done?
    >
    > All I know for the moment is this: srand(time(0)): rand(); but it is not
    > that great :-(


    For the range problem, look at the FAQ for the C language. You can find the
    FAQ via google.

    For the uniqueness problem use google to look for the magic buzzword
    "shuffle",
     
    osmium, Jan 26, 2010
    #2
    1. Advertising

  3. Larry

    osmium Guest

    osmium wrote:

    > "Larry" wrote:
    >
    >> I need to create a tiny function to generate a random number
    >> ranging from: 1000000 to 9999999. Also, the number should be unique
    >> even if I call the function 100 times in a second...can it actually
    >> be done? All I know for the moment is this: srand(time(0)): rand(); but
    >> it is
    >> not that great :-(

    >
    > For the range problem, look at the FAQ for the C language. You can
    > find the FAQ via google.
    >
    > For the uniqueness problem use google to look for the magic buzzword
    > "shuffle",


    On further thought. If you need a huge number of numbers, say over 100,000,
    the shuffle solution may not be a good one. In that case, establish a
    vector and put each number in it as chosen. The vector will grow
    automatically to "infinite" length, as needed. Examine the vector before
    issuing each number. The search will get slower and slower as time goes on
    but you may be able to clever that up a bit to handle that.
     
    osmium, Jan 26, 2010
    #3
  4. On 2010-01-26, Larry <> wrote:
    > Hi,
    >
    > I need to create a tiny function to generate a random number ranging
    > from: 1000000 to 9999999. Also, the number should be unique even if I call
    > the function 100 times in a second...can it actually be done?
    >
    > All I know for the moment is this: srand(time(0)): rand(); but it is not
    > that great :-(
    >


    Depending on how random you need this to be, you could use
    something simple, like:
    x = 1000000 + (x * Y mod 9000000);
    Where x is a static variable that you return with each call,
    and Y is anything reasonably big and relatively prime to
    9000000.

    That will look kinda random, and solve your uniqueness
    problem.

    This question might better be posed on comp.programming.
     
    Andrew Poelstra, Jan 26, 2010
    #4
  5. Larry

    James Kanze Guest

    On Jan 26, 1:54 pm, "Larry" <> wrote:

    > I need to create a tiny function to generate a random number
    > ranging from: 1000000 to 9999999. Also, the number should be
    > unique even if I call the function 100 times in a second...can
    > it actually be done?


    So which is it? Do the numbers have to be unique, or should
    they be random? (One excludes the other -- if they have to be
    unique, then they can't be truely random.)

    > All I know for the moment is this: srand(time(0)): rand(); but it is not
    > that great :-(


    The quality of the random number generated in the standard
    library is a quality of implementation issue. Some are fairly
    good, but a lot are horrid. It's fairly simple to implement
    your own, and even simpler to use Boost's functions.

    --
    James Kanze
     
    James Kanze, Jan 27, 2010
    #5
  6. On Jan 27, 9:25 am, James Kanze <> wrote:
    > On Jan 26, 1:54 pm, "Larry" <> wrote:
    >
    > > I need to create a tiny function to generate a random number
    > > ranging from: 1000000 to 9999999. Also, the number should be
    > > unique even if I call the function 100 times in a second...can
    > > it actually be done?

    >
    > So which is it?  Do the numbers have to be unique, or should
    > they be random?  (One excludes the other -- if they have to be
    > unique, then they can't be truely random.)
    >
    > > All I know for the moment is this: srand(time(0)): rand(); but it is not
    > > that great :-(

    >
    > The quality of the random number generated in the standard
    > library is a quality of implementation issue.  Some are fairly
    > good, but a lot are horrid.  It's fairly simple to implement
    > your own, and even simpler to use Boost's functions.
    >
    > --
    > James Kanze


    Try boost.random or GSL routines.
     
    Bo Schwarzstein, Jan 28, 2010
    #6
    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. Weng Tianxiang
    Replies:
    5
    Views:
    1,344
    Christophe
    Feb 16, 2006
  2. Varun  Kacholia
    Replies:
    11
    Views:
    2,920
    Patricia Shanahan
    Aug 3, 2006
  3. kkirtac
    Replies:
    18
    Views:
    625
    Jerry Coffin
    Sep 2, 2007
  4. remlostime

    generater big rand number

    remlostime, Oct 19, 2008, in forum: C++
    Replies:
    6
    Views:
    370
    Juha Nieminen
    Oct 20, 2008
  5. 7stud --

    rand() v. rand(0.1) ?

    7stud --, Sep 15, 2007, in forum: Ruby
    Replies:
    6
    Views:
    258
    Morton Goldberg
    Sep 16, 2007
Loading...

Share This Page