Re: Generating a random number

Discussion in 'C++' started by John Tsiombikas (Nuclear / the Lab), Jul 1, 2003.

  1. BEECHNUT88 wrote:
    > Hey,
    > I am having a problem with the program that I an trying to create. Im
    > trying to create a game where the computer generates a random number and then a
    > person has ten guesses to get the number. The number can only be four digits,
    > there cant be a duplicate any of the numbers in the random number. And it has
    > to be between 1023 and 9876. I have no idea how to do this. Can someone
    > pleasseee help me with this. My e-mail address is . Thank
    > you very much.
    >
    > Jimmy


    // a function that generates random numbers in the specified range
    int GenRandomNumber(int range_begin, int range_end) {
    int diff = abs(range_end - range_begin);
    return (rand() % diff) + range_begin;
    }

    Now for the thing about duplicates you say, I assume you mean that
    throughout the execution of your program you don't want to produce the
    same number twice, am I right?
    One simple way to do that would be to store the values you generated so
    far and if the new value is one of those then generate another one
    instead of returning the same. I would suggest the use of a hash table
    for storing the values due to its constant time search and add (the two
    operations you need).

    -- Nuclear / the Lab --
     
    John Tsiombikas (Nuclear / the Lab), Jul 1, 2003
    #1
    1. Advertising

  2. John Tsiombikas (Nuclear / the Lab)

    David White Guest

    "John Tsiombikas (Nuclear / the Lab)" <> wrote in
    message news:1057050769.370879@athprx02...
    > BEECHNUT88 wrote:
    > > Hey,
    > > I am having a problem with the program that I an trying to create.

    Im
    > > trying to create a game where the computer generates a random number and

    then a
    > > person has ten guesses to get the number. The number can only be four

    digits,
    > > there cant be a duplicate any of the numbers in the random number. And

    it has
    > > to be between 1023 and 9876. I have no idea how to do this. Can

    someone
    > > pleasseee help me with this. My e-mail address is .

    Thank
    > > you very much.
    > >
    > > Jimmy

    >
    > // a function that generates random numbers in the specified range
    > int GenRandomNumber(int range_begin, int range_end) {
    > int diff = abs(range_end - range_begin);
    > return (rand() % diff) + range_begin;
    > }


    For the purpose given this is probably okay, but "rand() % something" is
    slightly biased unless "something" divides exactly into rand()'s maximum
    value + 1.

    > Now for the thing about duplicates you say, I assume you mean that
    > throughout the execution of your program you don't want to produce the
    > same number twice, am I right?
    > One simple way to do that would be to store the values you generated so
    > far and if the new value is one of those then generate another one
    > instead of returning the same. I would suggest the use of a hash table
    > for storing the values due to its constant time search and add (the two
    > operations you need).


    If that's what's required then the problem seems the same as dealing cards,
    so you could also use a non-retrying card-dealer (using a vector and
    swapping dealt card with the deck's bottom card).

    David
     
    David White, Jul 1, 2003
    #2
    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. Leon

    Random Number Generating

    Leon, Oct 18, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    478
    Kevin Spencer
    Oct 18, 2004
  2. Wally
    Replies:
    1
    Views:
    2,839
    pvdg42
    Mar 20, 2006
  3. Marco de Boer

    Re: Generating a random number

    Marco de Boer, Jul 1, 2003, in forum: C++
    Replies:
    1
    Views:
    581
    J. Campbell
    Jul 2, 2003
  4. globalrev
    Replies:
    4
    Views:
    823
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,335
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page