problem with rand()
My friend & I wrote a war(card game) and the rand() function gives me the same number over and over.
#include
using namespace std;
int main() {
float money = 1000;
float wager = 50;
int card, c, compcard;
int winner;
cout << endl
<< "Your money:" << money
<< endl;
cout << endl
<< "Enter your wager( 50 or higher ):";
cin >> wager;
if( wager < 50 )
wager = 50;
if( wager > money )
wager = money;
for( c = 0; c < 10; c++) {
card = rand();
compcard = rand();
}
cout << endl
<< "Your card:" << card
<< endl;
cout << endl
<< "Computers card:" << compcard
<< endl;
if( card > compcard ) {
winner = 1;
} else if( card < compcard ) {
winner = 2;
} else {
winner = 3;
}
switch( winner ) {
case 1:
cout << endl
<< "You Won!"
<< endl;
money += wager;
break;
case 2:
cout << endl
<< "You Lost!"
<< endl;
money -= wager;
break;
case 3:
cout << endl
<< "You Tied!"
<< endl;
break;
}
if( money >= 1 ) {
cout << endl
<< "Your money:" << money
<< endl;
} else {
cout << endl
<< "You went broke!"
<< endl;
}
return 0;
}
I also want to know how to make it pick a number between 1 and 10. But if you can''t, I''ll stick to any random number.
Can anyone help me out?
KA...
ME...
HA...
ME...
HA!!!
KA...ME...HA...ME...HA!!!
you need to seed it.
Use srand() to do this.
This is the best way to get a totally random number.
this will give you a random number everytime.
Use srand() to do this.
This is the best way to get a totally random number.
#include <time.h>// .. codesrand((unsigned)time(NULL));rand()%value_here;
this will give you a random number everytime.
rand() inst really random, but uses a seed to produce random number, give it the same seed, and you get the same number.
Diemonex Games
hades.lodiz.nu/~mnb or
www.tsok.nu/diemonex
Diemonex Games
This isn''t very important, but I made an error when copying and pasting. It said #include at the top.
Sorry.
KA...
ME...
HA...
ME...
HA!!!
Sorry.
KA...
ME...
HA...
ME...
HA!!!
KA...ME...HA...ME...HA!!!
If you are using an older compiler you may have to use randomize(). Just though I''d point that out....
- Daniel
VG Games
- Daniel
VG Games
- DanielMy homepage
Another thing that should be mentioned is that rand() generally doesn''t produce a very good sequence of psuedom random numbers in the low bits. These tend to alternate (for example, the low bit might alternate between 0 and 1 with each call to rand()). So, although rand()%num is the easiest way to get a random number between 0 and num-1, it isn''t necessarily the best. Something that uses the high bits might be better. Or, I believe there is a function called random() that does a better job.
Just thought I''d throw in some things I''ve learned. Hope it helps!
Just thought I''d throw in some things I''ve learned. Hope it helps!
August 01, 2000 02:15 AM
In MSVC 6 you use the srand() function to seed the random number generator.
i.e. srand( (unsigned)time( NULL ) );
-Good luck
i.e. srand( (unsigned)time( NULL ) );
-Good luck
August 01, 2000 02:40 AM
How to make rand() pick a number between 1 and 10? Simple.
int card = rand() % 10;
This will give you a number between 0 and 9. Then just add 1.
int card = (rand() % 10) + 1;
int card = rand() % 10;
This will give you a number between 0 and 9. Then just add 1.
int card = (rand() % 10) + 1;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement