Re: Generating a random number

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

1. John Tsiombikas (Nuclear / the Lab)Guest

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

2. David WhiteGuest

"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