Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: Generating a random number (http://www.velocityreviews.com/forums/t267914-re-generating-a-random-number.html)

John Tsiombikas (Nuclear / the Lab) 07-01-2003 09:12 AM

Re: Generating a random number
 
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 BEECHNUT88@AOL.com. 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 --


David White 07-01-2003 10:38 AM

Re: Generating a random number
 
"John Tsiombikas (Nuclear / the Lab)" <nuclear@siggraph.org> 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 BEECHNUT88@AOL.com.

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





All times are GMT. The time now is 06:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.