Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/unit_test/random_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <boost/test/unit_test.hpp>
#include <explore_internal.h>
#include <rand_state.h>
#include "test_common.h"

BOOST_AUTO_TEST_CASE(reproduce_max_boundary_issue)
{
Expand All @@ -18,3 +20,14 @@ BOOST_AUTO_TEST_CASE(reproduce_max_boundary_issue)
float chosen_value = interval_size * (random_draw + 31) + range_min;
BOOST_CHECK_CLOSE(chosen_value, range_max, 0.000000001f);
}

BOOST_AUTO_TEST_CASE(check_rand_state_cross_platform)
{
rand_state random_state;
random_state.set_random_state(10);
BOOST_CHECK_CLOSE(random_state.get_and_update_random(), 0.0170166492, FLOAT_TOL);
BOOST_CHECK_CLOSE(random_state.get_and_update_random(), 0.370730162, FLOAT_TOL);
BOOST_CHECK_CLOSE(random_state.get_and_update_random(), 0.166691661, FLOAT_TOL);
BOOST_CHECK_CLOSE(random_state.get_and_update_random(), 0.362691283, FLOAT_TOL);
BOOST_CHECK_CLOSE(random_state.get_and_update_random(), 0.969445825, FLOAT_TOL);
}
13 changes: 6 additions & 7 deletions test/unit_test/simulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

namespace simulator
{
cb_sim::cb_sim(int seed)
: seed(seed)
, users({"Tom", "Anna"})
cb_sim::cb_sim(uint64_t seed)
: users({"Tom", "Anna"})
, times_of_day({"morning", "afternoon"})
, actions({"politics", "sports", "music", "food", "finance", "health", "camping"})
{
srand(seed);
random_state.set_random_state(seed);
}

float cb_sim::get_cost(const std::map<std::string, std::string>& context, const std::string& action)
Expand Down Expand Up @@ -57,7 +56,7 @@ std::pair<int, float> cb_sim::sample_custom_pmf(std::vector<float>& pmf)
float total = std::accumulate(pmf.begin(), pmf.end(), 0.f);
float scale = 1.f / total;
for (float& val : pmf) { val *= scale; }
float draw = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
float draw = random_state.get_and_update_random();
float sum_prob = 0.f;
for (int index = 0; index < pmf.size(); ++index)
{
Expand Down Expand Up @@ -87,13 +86,13 @@ std::pair<std::string, float> cb_sim::get_action(vw* vw, const std::map<std::str

const std::string& cb_sim::choose_user()
{
int rand_ind = rand() % users.size();
int rand_ind = static_cast<int>(random_state.get_and_update_random() * users.size());
return users[rand_ind];
}

const std::string& cb_sim::choose_time_of_day()
{
int rand_ind = rand() % times_of_day.size();
int rand_ind = static_cast<int>(random_state.get_and_update_random() * times_of_day.size());
return times_of_day[rand_ind];
}

Expand Down
5 changes: 3 additions & 2 deletions test/unit_test/simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <boost/test/test_tools.hpp>

#include "test_common.h"
#include "rand_state.h"

#include <functional>
#include <map>
Expand All @@ -29,12 +30,12 @@ class cb_sim
const std::vector<std::string> users;
const std::vector<std::string> times_of_day;
const std::vector<std::string> actions;
int seed;
rand_state random_state;
float cost_sum = 0.f;
std::vector<float> ctr;

public:
cb_sim(int = 0);
cb_sim(uint64_t = 0);
float get_cost(const std::map<std::string, std::string>&, const std::string&);
std::vector<std::string> to_vw_example_format(
const std::map<std::string, std::string>&, const std::string&, float = 0.f, float = 0.f);
Expand Down