Skip to content

Commit

Permalink
Use std::mt19937 to replace mt_random.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgc1112 committed Nov 25, 2023
1 parent 4d1fdcb commit 5e0cd11
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 196 deletions.
1 change: 1 addition & 0 deletions llbc/include/llbc/common/OSHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
#include <errno.h>
#include <time.h>
#include <math.h>
#include <random>
#if LLBC_TARGET_PLATFORM_NON_WIN32
#include <unistd.h>
#endif
Expand Down
3 changes: 1 addition & 2 deletions llbc/include/llbc/core/random/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#pragma once

#include "llbc/common/Common.h"
#include "llbc/core/random/mtrandom.h"

__LLBC_NS_BEGIN

Expand Down Expand Up @@ -90,7 +89,7 @@ class LLBC_EXPORT LLBC_Random
_RandomAccessIter Choice(const _RandomAccessIter &begin, const _RandomAccessIter &end);

private:
mtrandom _mtRand;
std::mt19937 _mtRand;
};

/**
Expand Down
22 changes: 10 additions & 12 deletions llbc/include/llbc/core/random/RandomInl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,22 @@ inline LLBC_Random::LLBC_Random(int seed)

inline void LLBC_Random::Seed(int seed)
{
_mtRand.reset(seed);
_mtRand.seed(seed);
}

inline int LLBC_Random::Rand()
{
return static_cast<int>(_mtRand.rand());
return static_cast<int>(_mtRand());
}

inline int LLBC_Random::Rand(int end)
{
if (LIKELY(end != 0))
{
const long long randVal = llabs(static_cast<int>(_mtRand.rand()));
if (end > 0)
return static_cast<int>(randVal % end);
return _mtRand() % end;
else
return static_cast<int>(randVal % end + end);
return _mtRand() % -end + end;
}
else
{
Expand All @@ -58,11 +57,10 @@ inline int LLBC_Random::Rand(int begin, int end)
{
if (LIKELY(begin != end))
{
const long long randVal = llabs(static_cast<int>(_mtRand.rand()));
if (begin < end)
return randVal % (end - begin) + begin;
return _mtRand() % (end - begin) + begin;
else
return randVal % (begin - end) + end;
return _mtRand() % (begin - end) + end;
}
else
{
Expand Down Expand Up @@ -98,19 +96,19 @@ LLBC_Random::Rand(const _Weights &weights)

inline double LLBC_Random::RandReal()
{
return _mtRand.real();
return static_cast<double>(_mtRand()) / 4294967296;

This comment has been minimized.

Copy link
@lailongwei

lailongwei Nov 25, 2023

Owner

可读性角度来看, 是否可调整为:
return static_cast<double>(_mtRand()) / UINT_MAX 或 后面的除数添加一个.?

}

inline bool LLBC_Random::BoolJudge()
{
return _mtRand.rand() % 2 == 1;
return _mtRand() % 2 == 1;

This comment has been minimized.

Copy link
@lailongwei

lailongwei Nov 25, 2023

Owner

不够直观,是否可以修改为_mtRand() % 1 != 0?

}

template <typename _RandomAccessIter>
inline _RandomAccessIter LLBC_Random::Choice(const _RandomAccessIter &begin, const _RandomAccessIter &end)
{
long diff = static_cast<long>(end - begin);
if (UNLIKELY(diff <= 0))
sint64 diff = static_cast<sint64>(end - begin);
if (UNLIKELY(diff <= 0 || diff > INT_MAX))

This comment has been minimized.

Copy link
@lailongwei

lailongwei Nov 25, 2023

Owner

因为mtrand返回是uint32(至少是uint32精度), 这边建议变成:
if (UNLIKELY(diff <= 0 || diff >= UINT_MAX))

return end;

return begin + Rand(diff);

This comment has been minimized.

Copy link
@lailongwei

lailongwei Nov 25, 2023

Owner

这行可直接变成:
return begin + _mtRand.rand() % diff

这样可兼容diff >= INT_MAX的情况

Expand Down
182 changes: 0 additions & 182 deletions llbc/include/llbc/core/random/mtrandom.h

This file was deleted.

0 comments on commit 5e0cd11

Please sign in to comment.