Skip to content
Permalink
Browse files
GEODE-2525: Removes MersenneTwister sources.
  • Loading branch information
pivotal-jbarrett committed Jun 24, 2017
1 parent d4fe45d commit 4999c79104ccc50d658add486834654e75823f8f
Showing 7 changed files with 45 additions and 698 deletions.
36 LICENSE
@@ -209,39 +209,3 @@ with separate copyright notices and license terms. Your use of those
components are subject to the terms and conditions of the following
licenses.

---------------------------------------------------------------------------
The BSD 3-Clause License (http://opensource.org/licenses/BSD-3-Clause)
---------------------------------------------------------------------------

Apache Geode bundles the following files under the BSD 3-Clause License:

- MersenneTwister.*pp (ihttp://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html),
Copyright (c) 1997-2002, Makoto Matsumoto and Takuji Nishimura
Copyright (c) 2000-2003, Richard J. Wagner

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -18,60 +18,15 @@
#include "GsRandom.hpp"

#include <cstring>
#include <mutex>
#include <util/concurrent/spinlock_mutex.hpp>

namespace apache {
namespace geode {
namespace client {
namespace testframework {

using util::concurrent::spinlock_mutex;

GsRandom *GsRandom::singleton = 0;
MTRand GsRandom::gen;
int32_t GsRandom::seedUsed = -101;
spinlock_mutex GsRandom::lck;

/**
* Creates a new random number generator using a single
* <code>int32_t</code> seed.
*
* @param seed the initial seed.
* @see java.util.Random#Random(int32_t)
*/
GsRandom *GsRandom::getInstance(int32_t seed) {
if (singleton == 0) {
setInstance(seed);
} else {
std::lock_guard<spinlock_mutex> guard(lck);
setSeed(seed);
}
return singleton;
}

void GsRandom::setInstance(int32_t seed) {
std::lock_guard<spinlock_mutex> guard(lck);
if (singleton == 0) {
singleton = new GsRandom();
if (seed != -1) {
singleton->gen.seed(seed);
} else {
singleton->gen.seed();
}
seedUsed = seed;
}
}

void GsRandom::setSeed(int32_t seed) {
if (seed != seedUsed) {
if (seed != -1) {
singleton->gen.seed(seed);
} else {
singleton->gen.seed();
}
seedUsed = seed;
}
GsRandom &GsRandom::getInstance() {
static GsRandom instance;
return instance;
}

/**
@@ -23,78 +23,63 @@
#include <geode/geode_base.hpp>

#include <string>

#include <util/concurrent/spinlock_mutex.hpp>
#include "MersenneTwister.hpp"
#include <random>

namespace apache {
namespace geode {
namespace client {
namespace testframework {

using util::concurrent::spinlock_mutex;

class GsRandom {
private:
static MTRand gen;
static GsRandom* singleton;
static int32_t seedUsed;
static spinlock_mutex lck;
static void setInstance(int32_t seed);

GsRandom() {}
static void setSeed(int32_t seed);

public:
~GsRandom() {
if (singleton != NULL) {
delete singleton;
singleton = NULL;
}
std::default_random_engine engine;

std::uniform_int_distribution<> distBoolean;
std::uniform_int_distribution<uint8_t> distUint8;
std::uniform_int_distribution<uint16_t> distUint16;
std::uniform_int_distribution<int32_t> distInt32;
std::uniform_int_distribution<uint32_t> distUint32;
std::uniform_real_distribution<double> distDouble;

GsRandom()
: distBoolean(0, 1),
distUint8(),
distUint16(),
distInt32(),
distUint32(),
distDouble() {
std::random_device seed;
engine = std::default_random_engine(seed());
}
~GsRandom() = default;
GsRandom(const GsRandom&) = delete;
GsRandom& operator=(const GsRandom&) = delete;

public:
/**
* Creates a new random number generator. Its seed is initialized to
* a value based on the /dev/urandom or current time.
*
* @see java.lang.System#currentTimeMillis()
* @see java.util.Random#Random()
*/
inline static GsRandom* getInstance() {
if (singleton == 0) setInstance(-1);
return singleton;
}

/**
* Creates a new random number generator using a single
* <code>int32_t</code> seed.
* a value based on the random device.
*
* @param seed the initial seed.
* @see java.util.Random#Random(int32_t)
*/
static GsRandom* getInstance(int32_t seed);
static GsRandom& getInstance();

/**
* @return the next pseudorandom, uniformly distributed <code>boolean</code>
* value from this random number generator's sequence.
*/
inline bool nextBoolean() { return (singleton->gen.randInt(1) == 0); }
inline bool nextBoolean() { return 1 == distBoolean(engine); }

/**
* @return the next pseudorandom, uniformly distributed <code>uint16_t</code>
* value from this random number generator's sequence.
*/
inline uint16_t nextInt16() {
return static_cast<uint16_t>(singleton->gen.randInt(0xffff));
}
inline uint16_t nextInt16() { return distUint16(engine); }

/**
* @return the next pseudorandom, uniformly distributed <code>byte</code>
* value from this random number generator's sequence.
*/
inline uint8_t nextByte() {
return static_cast<uint8_t>(singleton->gen.randInt(0xff));
}
inline uint8_t nextByte() { return distUint8(engine); }

/**
* @param min the minimum range (inclusive) for the pseudorandom.
@@ -103,9 +88,9 @@ class GsRandom {
* value from this random number generator's sequence.
* If max < min, returns 0 .
*/
inline uint8_t nextByte(int32_t min, int32_t max) {
if (max < min) return 0;
return static_cast<uint8_t>(singleton->gen.randInt(max - min) + min);
inline uint8_t nextByte(uint8_t min, uint8_t max) {
return distUint8(
engine, std::uniform_int_distribution<uint8_t>::param_type(min, max));
}

/**
@@ -123,7 +108,8 @@ class GsRandom {
* from min to max.
*/
inline double nextDouble(double min, double max) {
return (singleton->gen.rand(max - min) + min);
return distDouble(
engine, std::uniform_real_distribution<double>::param_type(min, max));
}

/**
@@ -141,24 +127,23 @@ class GsRandom {
* If max < min, returns 0 .
*/
inline int32_t nextInt(int32_t min, int32_t max) {
if (max < min) return 0;
return singleton->gen.randInt(max - min) + min;
return distInt32(
engine, std::uniform_int_distribution<int32_t>::param_type(min, max));
}

/** @brief return random number where: min <= retValue < max */
static uint32_t random(uint32_t min, uint32_t max) {
return static_cast<uint32_t>(
GsRandom::getInstance()->nextInt(min, max - 1));
return getInstance().distUint32(
getInstance().engine,
std::uniform_int_distribution<uint32_t>::param_type(min, max - 1));
}

/** @brief return random number where: 0 <= retValue < max */
static uint32_t random(uint32_t max) {
return static_cast<uint32_t>(GsRandom::getInstance()->nextInt(0, max - 1));
}
static uint32_t random(uint32_t max) { return random(0, max - 1); }

/** @brief return random double where: min <= retValue <= max */
static double random(double min, double max) {
return GsRandom::getInstance()->nextDouble(min, max);
return getInstance().nextDouble(min, max);
}

/** @brief return bounded random string,
@@ -198,14 +183,6 @@ class GsRandom {
}
}

// /**
// * Returns a randomly-selected element of Vector vec.
// */
// inline void * randomElement(Vector vec)
// {
// return (void *)(vec.at(nextInt(vec.size())));
// }

/**
* @param max the maximum length of the random string to generate.
* @return a bounded random string with a length between 0 and

0 comments on commit 4999c79

Please sign in to comment.