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

X

xeys_00

#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.
 
J

Johannes Bauer

xeys_00 said:
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-----
 
W

wwwolf

Johannes said:
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
 
J

Johannes Bauer

wwwolf said:
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).
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+h3WmnWoEFJBzq/KmwCgkA4P
ULAG03W9y3zzeO4HlIq4Hg0=
=Nskj
-----END PGP SIGNATURE-----
 
W

wwwolf

Evan said:
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=FreeBSD+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!!!
 
W

wwwolf

Johannes Bauer wrote:
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!
 
X

xeys_00

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
 
P

Peter Koch Larsen

"Johannes Bauer" <[email protected]> skrev i en meddelelse
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
 
J

Johannes Bauer

Peter said:
"Johannes Bauer" <[email protected]> skrev i en meddelelse



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*.

You're right, I was thinking in C, not C++.
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.

Why is that? Doesn't that defy all object-oriented concepts? I mean,
using pointers alone isn't very OO, but using integer numbers as
pointers is, well, awkward.

What reason would there be to use 0 in favor of NULL?

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

iD8DBQFCWTQCCseFG8xyfi4RAll5AKCcUwTZRpoMoG9JUvr0J6I/Hy8OagCgnasK
vI63lPWUQeIPvBvrVErQ4sM=
=aZBO
-----END PGP SIGNATURE-----
 
R

Ravi

Johannes said:
What reason would there be to use 0 in favor of NULL?

int main()
{
int *p = NULL;
return 0;
}

is a program that will not compile

int main()
{
int *p = 0;
return 0;
}

is a program that will compile. it is not the same as:

int main()
{
int main()
{
int x = 0;
int *p = x;
return 0;
}

because the compiler cannot guarantee x will be 0. There are two
special things about the value 0:
1) You can assign a pointer to it with no cast for any pointer type.
2) A lot of code is written to check for equality to 0 before using the
pointer. deletion of a pointer that happens to equal to 0 is also
checked and safe too.

NULL is just a
#define NULL 0
rather idiotic to use especially because its in some random header file.
 
J

Jonathan Arnold

As a corollary to this srand/rand question, a colleague of mine
has this code:

// Initialize the random number generator
uint32_t seed; seed += getpid() * time(NULL);
srand(seed);
int n = (seed >> 16) & 0xFFFF;
for (int i = 0; i < n; ++i) rand();

When I asked him if calling rand a random number of times would
generate a more random number, his answer was that it makes the
number "less predictable but not more random." Not sure I
understand the subtleness of the answer, but is it true?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top