Random numbers not working
Apr 17, 2012 at 2:07pm UTC
Ok, I decided to create a small program today to test out some new stuff I had learned, and for some reason, the random number generator always generates 18. I've tried moving srand(time(0)); to different places so It would only be called once, and it didn't work. Please take a look!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;
int main()
{
int ans, random, evil, total;
int male = 1;
int female = 1;
int vampire = 0;
cout<<"Press 1 to start and 2 to exit" <<endl;
cin >> ans;
if (ans == 1){
//main
cout<<"Press 1 to begin the day and 2 to end" <<endl;
cin >> ans;
if (ans == 1){
//start program
bool start = true ;
srand(time(0));
while (start == true ){
//day
if (male >= 1 && female >= 1){
Sleep(2000);
random == rand() %10 + 1;
if (random >= 1 && random <= 20){
//spawn male
male++;
total = male + female;
cout<<endl;
start == false ;
}
if (random >= 80 && random <= 100){
//spawm female
female++;
total = male + female;
cout<<endl;
start == false ;
}
if (random >= 101 && random <= 105){
cout<<"VAMPIRE SHEEP" <<endl;
srand(time(0));
evil == rand() %20 + 1;
total = total - random;
vampire++;
start == false ;
}
if (random >= 21 && random <= 99){
cout<<"No sheep today" <<endl;
start == false ;
}
}
while (vampire > 1){
if (random >= 1 && random <= 5){
cout<<"Evil sheep is dead" <<endl;
vampire--;
cout<<"There are " <<vampire<<" evil sheep left" <<endl;
start == false ;
}
if (random >= 6 && random <= 10){
cout<<"Evil sheep still alive" <<endl;
start == false ;
}
}
cout<<"Random: " <<random<<endl;
cout<<endl;
cout<<"Male: " <<male<<endl;
cout<<endl;
cout<<"Female: " <<female<<endl;
cout<<endl;
start == false ;
}
}
if (ans == 2){
//exit program
cout<<"Thanks for playing!" <<endl;
}
}
if (ans == 2){
//exit program
cout<<"Thanks for playing!" <<endl;
}
system("PAUSE" );
return 0;
}
Thanks!
EDIT: srand is on line 28. I don't know why I put it there, I've been trying different places in the code.
Last edited on Apr 17, 2012 at 2:08pm UTC
Apr 17, 2012 at 2:13pm UTC
Why are you using == for assigments .
Use a single = for all your assignments.
Apr 17, 2012 at 2:14pm UTC
Whoops haha sorry. I'll fix that.
Topic archived. No new replies allowed.