Skip to content

Commit

Permalink
add C-11 random feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeLing authored and vigsterkr committed Feb 22, 2018
1 parent 1d15fc9 commit fa99296
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/shogun/base/SGObject.cpp
Expand Up @@ -510,7 +510,7 @@ void CSGObject::init()
m_parameters = new Parameter();
m_model_selection_parameters = new Parameter();
m_gradient_parameters=new Parameter();
m_rng = std::unique_ptr<CRandom>(new CRandom(sg_random_seed));
m_rng = std::unique_ptr<CRandom>(new CRandom());
m_generic = PT_NOT_GENERIC;
m_load_pre_called = false;
m_load_post_called = false;
Expand Down
29 changes: 1 addition & 28 deletions src/shogun/base/init.cpp
Expand Up @@ -34,15 +34,6 @@ shogun::CMap<void*, shogun::MemoryBlock>* sg_mallocs=NULL;
#include <google/protobuf/stubs/common.h>
#endif

#ifdef _WIN32
#define _CRT_RAND_S
#include <stdlib.h>
#endif

#ifdef DEV_RANDOM
#include <fcntl.h>
#endif

namespace shogun
{
Parallel* sg_parallel=NULL;
Expand Down Expand Up @@ -272,25 +263,7 @@ namespace shogun

uint32_t generate_seed()
{
uint32_t seed;
#if defined(_WIN32)
rand_s(&seed);
#elif defined(HAVE_ARC4RANDOM)
seed = arc4random();
#elif defined(DEV_RANDOM)
int fd = open(DEV_RANDOM, O_RDONLY);
ASSERT(fd >= 0);
ssize_t actual_read =
read(fd, reinterpret_cast<char*>(&seed), sizeof(seed));
close(fd);
ASSERT(actual_read == sizeof(seed));
#else
SG_SWARNING("Not safe seed for the PRNG\n");
struct timeval tv;
gettimeofday(&tv, NULL);
seed = (uint32_t)(4223517 * getpid() * tv.tv_sec * tv.tv_usec);
#endif
return seed;
return std::random_device()();
}

void set_global_seed(uint32_t seed)
Expand Down
35 changes: 23 additions & 12 deletions src/shogun/base/init.h
Expand Up @@ -12,6 +12,7 @@
#include <shogun/lib/config.h>

#include <functional>
#include <random>
#include <stdio.h>

namespace shogun
Expand All @@ -23,6 +24,7 @@ namespace shogun
class CRandom;
class SGLinalg;
class CSignal;
extern uint32_t sg_random_seed;

/** This function must be called before libshogun is used. Usually shogun
* does
Expand All @@ -33,9 +35,7 @@ namespace shogun
* @param print_message function pointer to print a message
* @param print_warning function pointer to print a warning message
* @param print_error function pointer to print an error message (this will
* be
* printed before shogun throws an
* exception)
* be printed before shogun throws an exception)
*
* @param cancel_computations function pointer to check for exception
*
Expand Down Expand Up @@ -123,17 +123,28 @@ namespace shogun
*/
CMath* get_global_math();

/** Set global random seed
* @param seed seed for random generator
*/
void set_global_seed(uint32_t seed);
/** Set global random seed
* @param seed seed for random generator
*/
void set_global_seed(uint32_t seed);

/** get global random seed
* @return random seed
*/
uint32_t get_global_seed();
/** get global random seed
* @return random seed
*/
uint32_t get_global_seed();

/**
* Generate a seed for PRNG
*
* @return entropy for PRNG
*/
uint32_t generate_seed();

uint32_t generate_seed();
template <typename T = std::mt19937>
T get_prng()
{
return T(sg_random_seed);
}

#ifndef SWIG // SWIG should skip this part
/** get the global linalg library object
Expand Down
3 changes: 1 addition & 2 deletions src/shogun/mathematics/Math.h
Expand Up @@ -820,8 +820,7 @@ class CMath : public CSGObject
}
else
{
auto m_rng =
std::unique_ptr<CRandom>(new CRandom());
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
for (index_t i = 0; i < v.vlen; ++i)
swap(v[i], v[m_rng->random(i, v.vlen - 1)]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/shogun/multiclass/LaRank.h
Expand Up @@ -249,8 +249,7 @@ namespace shogun

LaRankPattern & sample ()
{
auto m_rng =
std::unique_ptr<CRandom>(new CRandom());
auto m_rng = std::unique_ptr<CRandom>(new CRandom());
ASSERT(!empty())
while (true)
{
Expand Down

0 comments on commit fa99296

Please sign in to comment.