For #include <files>
────────────────────
If you find yourself using
std::string anywhere in your .cpp file, you should have
#include <string>
at the top. It does not matter whether the file is known to be included by some other header. If you use it, include it.
For IDE stuff
────────────────────
Find something you like and feel comfortable with. The IDE will not make all your problems go away (and would not have helped you here), but it does make life easier. Personally I like to use a good plain-text editor and compile everything at the command line.
If you do use an IDE, make sure to configure it to use the latest C++ standard when frobbing the compiler (C++17 if possible, C++11 at the absolute minimum).
For random stuff
────────────────────
The
<chrono> library is a nice C++ wrapper over
<ctime>.
If you click on the “
Search” box at the top of the page you can type in “random” and the site search will typically bring up a good place to start reading. In this case, it brings up the page for the
<random> library.
I scrolled down a little and chose the “ranlux24” generator and clicked it.
Then I clicked the link that says “(constructor)”, bringing me to this page:
http://www.cplusplus.com/reference/random/discard_block_engine/discard_block_engine/
...which has a very convenient collection of examples for using the PRNG. The one most useful for you is:
1 2 3 4 5 6 7 8 9 10 11
|
#include <chrono>
#include <iostream>
#include <random>
int main()
{
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::ranlux24 g4(seed);
std::cout << "g4(): " << g4() << std::endl;
}
|
A PRNG is one of the few objects you should feel free to make a global. Hence:
1 2
|
#include <chrono>
#include <random>
|
1 2 3 4 5 6
|
template <typename Integer>
Integer random( Integer min, Integer max )
{
static std::ranlux24 rng( std::chrono::system_clock::now().time_since_epoch().count() );
return std::uniform_int_distribution <int> ( min, max )( rng );
}
|
|
level = random( 1, player.getLevel() );
|
Clicking on the “(constructor)” link in the documentation (both here and at cppreference.com) typically takes you to the best place to find example code.
Hope this helps.