By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

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

P: n/a

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

Jul 23 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
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-----

Jul 23 '05 #3

P: n/a
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

Jul 23 '05 #4

P: n/a
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-----

Jul 23 '05 #5

P: n/a
>> 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

Jul 23 '05 #6

P: n/a
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!!!
Jul 23 '05 #7

P: n/a
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!
Jul 23 '05 #8

P: n/a
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

Jul 23 '05 #9

P: n/a

"Johannes Bauer" <df***********@gmx.de> skrev i en meddelelse
news:2e************@snifftop.sniffdomain...

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

Jul 23 '05 #10

P: n/a
Peter Koch Larsen wrote:
"Johannes Bauer" <df***********@gmx.de> skrev i en meddelelse
news:2e************@snifftop.sniffdomain...

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


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

iD8DBQFCWTQCCseFG8xyfi4RAll5AKCcUwTZRpoMoG9JUvr0J6 I/Hy8OagCgnasK
vI63lPWUQeIPvBvrVErQ4sM=
=aZBO
-----END PGP SIGNATURE-----

Jul 23 '05 #11

P: n/a
Johannes Bauer wrote:
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.

Jul 23 '05 #12

P: n/a
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?

--
Jonathan Arnold (mailto:jd******@buddydog.org)
The Incredible Brightness of Seeing, a Home Theater weblog
http://www.anaze.us/HomeTheater
Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.