Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Putting using namespace std at the top of every program is a bad habita bad habit that you'd do well to avoid. It's an alarmingly common thing for new C++ programmers to do.

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. It's an alarmingly common thing for new C++ programmers to do.

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. It's an alarmingly common thing for new C++ programmers to do.

fixed variable name and grammar
Source Link
Edward
  • 66.6k
  • 4
  • 118
  • 282
random1actualColour = rand() % 2;

There are two problems with this approach. One is that the low order bits of the random number generator are not particularly random, so neither withwill random1actualColour be. On my machine, there's a slight but measurable bias toward 0 with that. The second problem is that it's not thread safe because rand stores hidden state. A better solution, if your compiler and library supports it, would be to use the C++11 `std::uniform_int_distribution. It looks complex, but it's actually pretty easy to use.

random1 = rand() % 2;

There are two problems with this approach. One is that the low order bits of the random number generator are not particularly random, so neither with random1 be. On my machine, there's a slight but measurable bias toward 0 with that. The second problem is that it's not thread safe because rand stores hidden state. A better solution, if your compiler and library supports it, would be to use the C++11 `std::uniform_int_distribution. It looks complex, but it's actually pretty easy to use.

actualColour = rand() % 2;

There are two problems with this approach. One is that the low order bits of the random number generator are not particularly random, so neither will actualColour be. On my machine, there's a slight but measurable bias toward 0 with that. The second problem is that it's not thread safe because rand stores hidden state. A better solution, if your compiler and library supports it, would be to use the C++11 `std::uniform_int_distribution. It looks complex, but it's actually pretty easy to use.

removed cut-and-paste error
Source Link
Edward
  • 66.6k
  • 4
  • 118
  • 282

Eliminate unused variables

This code declares a variable event1 but then does nothing with it. Your compiler is smart enough to help you find this kind of problem if you know how to ask it to do so.

Don't use system("pause")

Eliminate unused variables

This code declares a variable event1 but then does nothing with it. Your compiler is smart enough to help you find this kind of problem if you know how to ask it to do so.

Don't use system("pause")

Don't use system("pause")

Source Link
Edward
  • 66.6k
  • 4
  • 118
  • 282
Loading