Use C++ stdlib for random number generation

This commit is contained in:
Max Brunsfeld 2017-08-08 12:42:49 -07:00
parent fc0f49e4ee
commit b43ae2826b
4 changed files with 35 additions and 12 deletions

View file

@ -1,22 +1,40 @@
#include <string>
#include <vector>
#include <stdlib.h>
#include <random>
#include <time.h>
using std::string;
using std::vector;
static std::default_random_engine engine;
unsigned get_time_as_seed() {
return time(nullptr);
}
void random_reseed(unsigned seed) {
engine.seed(seed);
}
unsigned random_unsigned() {
return std::uniform_int_distribution<unsigned>()(engine);
}
unsigned random_unsigned(unsigned max) {
return std::uniform_int_distribution<unsigned>(0, max - 1)(engine);
}
static string random_string(char min, char max) {
string result;
size_t length = random() % 12;
size_t length = random_unsigned(12);
for (size_t i = 0; i < length; i++) {
char inserted_char = min + (random() % (max - min));
result += inserted_char;
result += (min + random_unsigned(max - min));
}
return result;
}
static string random_char(string characters) {
size_t index = random() % characters.size();
size_t index = random_unsigned(characters.size());
return string() + characters[index];
}
@ -24,7 +42,7 @@ string random_words(size_t count) {
string result;
bool just_inserted_word = false;
for (size_t i = 0; i < count; i++) {
if (random() % 10 < 6) {
if (random_unsigned(10) < 6) {
result += random_char("!(){}[]<>+-=");
} else {
if (just_inserted_word)
@ -37,5 +55,5 @@ string random_words(size_t count) {
}
string select_random(const vector<string> &list) {
return list[random() % list.size()];
return list[random_unsigned(list.size())];
}

View file

@ -4,6 +4,10 @@
#include <string>
#include <vector>
unsigned get_time_as_seed();
void random_reseed(unsigned);
unsigned random_unsigned();
unsigned random_unsigned(unsigned max);
std::string random_words(size_t count);
std::string select_random(const std::vector<std::string> &);

View file

@ -75,9 +75,9 @@ for (auto &language_name : test_languages) {
set<pair<size_t, string>> insertions;
for (size_t i = 0; i < 60; i++) {
size_t edit_position = random() % utf8_char_count(entry.input);
size_t deletion_size = random() % (utf8_char_count(entry.input) - edit_position);
string inserted_text = random_words(random() % 4 + 1);
size_t edit_position = random_unsigned(utf8_char_count(entry.input));
size_t deletion_size = random_unsigned(utf8_char_count(entry.input) - edit_position);
string inserted_text = random_words(random_unsigned(4) + 1);
if (insertions.insert({edit_position, inserted_text}).second) {
string description = "\"" + inserted_text + "\" at " + to_string(edit_position);

View file

@ -1,4 +1,5 @@
#include "test_helper.h"
#include "helpers/random_helpers.h"
int main(int argc, char *argv[]) {
int seed;
@ -6,11 +7,11 @@ int main(int argc, char *argv[]) {
if (seed_env) {
seed = atoi(seed_env);
} else {
seed = time(nullptr);
seed = get_time_as_seed();
}
printf("Random seed: %d\n", seed);
srandom(seed);
random_reseed(seed);
return bandit::run(argc, argv);
}