Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > My random number is only random for the first run???

Reply
Thread Tools

My random number is only random for the first run???

 
 
xeys_00
Guest
Posts: n/a
 
      04-09-2005

#include<iostream>
#include<cstdlib>



//This program will be a number guessing game

int main()
{
//variables for the game
int number_of_tries=0;//how many times did it take to get it right?
int current_guess=0;//the number being currently tried
int upper_limit=100;
int lower_limit=1;
int correct_number=rand()%100;//the correct number will be from no
higher than 100
while (correct_number != current_guess)
{
//user interaction
std::cout << "What is your guess? The correct number will be no higher
than 100, or lower than 1.\n";
std::cin >> current_guess;//user inputs his guess
if (current_guess > upper_limit || current_guess < lower_limit)
{
std::cout << "What, you can't read directions? The number has to be
no higher than 100, or lower than 1!!! You're Fired!!!!!\n";
std::abort();//kicks user out if input is bad
}
else if (current_guess < correct_number)
{
std::cout << "Your guess is too low. Try again\n";
number_of_tries++;
}
else if (current_guess > correct_number)
{
std::cout << "Your guess is too high. Try again\n";
number_of_tries++;
}
else if (current_guess == correct_number)
{
std::cout << "Good job! You guessed the right number!!!\n";
std::cout << "It only took you "<< number_of_tries << " tries!\n";
break;
}
}

return 0;


}


All is good for the first run. Then the wierdest thing happens... The
"random" number is always the same forever afterwards.

 
Reply With Quote
 
 
 
 
CrayzeeWulf
Guest
Posts: n/a
 
      04-09-2005
xeys_00 wrote:
> All is good for the first run. Then the wierdest thing happens... The
> "random" number is always the same forever afterwards.

The function rand() generates pseudo-random numbers. You need to use srand()
to seed it with a different seed every time you run the program in order to
generate different sequences of random number. See the following:

http://cplus.about.com/od/cprogrammi.../aa041403b.htm

Later,
--
CrayzeeWulf
 
Reply With Quote
 
 
 
 
Johannes Bauer
Guest
Posts: n/a
 
      04-09-2005
xeys_00 wrote:
> int correct_number=rand()%100;//the correct number will be from no
> higher than 100


You're not initializing the random seed. Do something like

srand(time(NULL));

at the very start and you'll have more "random" random numbers (although
still remaining highly deterministic).

Although in this very application not really necessary, you should
follow the advice from rand(3) how to avoid using lower-order bits in
your random numbers.

Greetings,
Johannes

--
PLEASE verify my signature. Some forging troll is claiming to be me.
My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
Also: Messages from "Comcast Online" are ALWAYS forged.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCWGe5CseFG8xyfi4RAvMxAJsE/6CeRJdHiBXgdOGyPmmhHX5JUgCfTu1z
wH+EA75o+BSr6p9RTKQVwps=
=xX7J
-----END PGP SIGNATURE-----

 
Reply With Quote
 
wwwolf
Guest
Posts: n/a
 
      04-10-2005
Johannes Bauer wrote:
> Do something like
>
> srand(time(NULL));
>


Is there any difference at all between srand(time(NULL)); and srand(time(0));?


> you should
> follow the advice from rand(3) how to avoid using lower-order bits in
> your random numbers.


Where is this advice, and how do I get it?

Thanx for your help,

wwwolf

 
Reply With Quote
 
Johannes Bauer
Guest
Posts: n/a
 
      04-10-2005
wwwolf wrote:
> Johannes Bauer wrote:
>
>>Do something like
>>
>>srand(time(NULL));
>>

> Is there any difference at all between srand(time(NULL)); and srand(time(0));?


Well, yes. 0 is the number zero. NULL is the pointer zero, usually
defined as "(void*)0". The time(2) call is system specific; however on
my box, a Linux system, it demands a "time_t*" as an argument. It
requires a pointer. NULL is a pointer, 0 is not.

So when you want to go safe, use NULL, although "0" might work when your
compiler doesn't take type-safety too seriously (which _you_ should).

>>you should
>>follow the advice from rand(3) how to avoid using lower-order bits in
>>your random numbers.

>
> Where is this advice, and how do I get it?


Try typing "man 3 rand" - when you've manual pages installed it comes
up. When you don't, here it is:

------------
In Numerical Recipes in C: The Art of Scientific Computing (William
H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling;
New York: Cambridge University Press, 1992 (2nd ed., p. 277)), the
following comments are made:

"If you want to generate a random integer between 1 and 10, you should
always do it by using high-order bits, as in

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."
------------

Greetings,
Johannes

--
PLEASE verify my signature. Some forging troll is claiming to be me.
My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
Also: Messages from "Comcast Online" are ALWAYS forged.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCWJKiCseFG8xyfi4RAmMyAJ9Hg+plvUgD+h3WmnWoEF JBzq/KmwCgkA4P
ULAG03W9y3zzeO4HlIq4Hg0=
=Nskj
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Evan
Guest
Posts: n/a
 
      04-10-2005
>> you should
>> follow the advice from rand(3) how to avoid using lower-order bits

in
>> your random numbers.

>Where is this advice, and how do I get it?


I'm going to go out on a limb and guess that since you don't know about
the rand(3) style syntax, you're not on a *nix platform and Johannes's
instructions to type "man 3 rand" will be met with an error. In any
case, his assumption that you have man pages is quite unreasonable.

See
http://www.freebsd.org/cgi/man.cgi?q...ts&format=html

 
Reply With Quote
 
wwwolf
Guest
Posts: n/a
 
      04-10-2005
Evan wrote:

>>> you should
>>> follow the advice from rand(3) how to avoid using lower-order bits

> in
>>> your random numbers.

>>Where is this advice, and how do I get it?

>
> I'm going to go out on a limb and guess that since you don't know about
> the rand(3) style syntax, you're not on a *nix platform and Johannes's
> instructions to type "man 3 rand" will be met with an error. In any
> case, his assumption that you have man pages is quite unreasonable.
>
> See
> http://www.freebsd.org/cgi

man.cgi?query=rand&apropos=0&sektion=3&manpath=Fre eBSD+5.3-RELEASE+an
+Ports&format=html

OK, I must have had a brain fart. I wasn't thinking man at the time. rand(3)
looked too much like function() and I got a little confused.

Thanx VERY MUCH for that link. I think I will be using it often!!!


 
Reply With Quote
 
wwwolf
Guest
Posts: n/a
 
      04-10-2005
Johannes Bauer wrote:
<snip>
> Try typing "man 3 rand" - when you've manual pages installed it comes
> up.


Thanx, I wasn't aware that the man pages could explain functions to me. This has
opened up a whole new resource for me!


 
Reply With Quote
 
xeys_00
Guest
Posts: n/a
 
      04-10-2005
Well, thanks much. I will definitely play around with this and probably
add stuff to the prog. But I want to get the basic functionality
working first.

Xeys

 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      04-10-2005

"Johannes Bauer" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...

wwwolf wrote:
> Johannes Bauer wrote:
>
>>Do something like
>>
>>srand(time(NULL));
>>

> Is there any difference at all between srand(time(NULL)); and
> srand(time(0));?


Johannes Baquer also wrote:
"Well, yes. 0 is the number zero. NULL is the pointer zero, usually
defined as "(void*)0". The time(2) call is system specific; however on
my box, a Linux system, it demands a "time_t*" as an argument. It
requires a pointer. NULL is a pointer, 0 is not.

So when you want to go safe, use NULL, although "0" might work when your
compiler doesn't take type-safety too seriously (which _you_ should)."

That is simply not true. First NULL simply is not defined as (void*)0 as
this would simply not work. In C++ there is no implicit conversion from
void*.
Secondly, 0 is the (only possible) representation for a null-pointer and is
both portable and type-safe. In fact you will find many (Stroustrup is one
of these) who recommend you use 0, not NULL.

/Peter



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
How do I get a random number between two random numbers? Alex Untitled Ruby 11 11-16-2009 09:45 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
Random NOt random? Darren Clark ASP .Net 3 06-24-2004 05:23 PM



Advertisments