Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
4003CEM-classes_orig/randomvalues.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
53 lines (43 sloc)
1.3 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef RANDOMNAME_H | |
#define RANDOMNAME_H | |
#include <vector> | |
#include <string> | |
#include <fstream> | |
#include <random> | |
#include <exception> | |
#include <iostream> | |
class RandomValues | |
{ | |
private: | |
std::vector<std::string> names; | |
public: | |
void load_names( const char* filename="names.txt" ) | |
{ | |
// open the names file | |
std::ifstream file; | |
file.open( filename ); | |
// read the names file | |
std::string buffer; | |
while( getline( file, buffer ) ) | |
names.emplace_back( std::move(buffer) ); | |
} | |
RandomValues() { load_names(); } | |
/** select a random name from the values loaded into memory **/ | |
std::string name() const | |
{ | |
if( names.empty() ) throw std::runtime_error( "No names loaded" ); | |
std::random_device generator; | |
std::uniform_int_distribution<size_t> distribution( 0, names.size()-1 ); | |
return *next( begin(names), distribution(generator) ); | |
} | |
/** generate a random damage amount | |
* damage is affected by supplied level, higher levels will produce higher | |
* damages on average **/ | |
int damage( int level ) const | |
{ | |
std::random_device generator; | |
std::uniform_int_distribution<int> distribution( level*1000, 20000 ); | |
return distribution( generator ); | |
} | |
}; | |
#endif |