Skip to content

Commit

Permalink
Util/Random: Add randtime(Milliseconds const&, Milliseconds const&) t…
Browse files Browse the repository at this point in the history
…o supersede urandms(uint32,uint32) for scripts being ported to std::chrono.
  • Loading branch information
Treeston committed Mar 1, 2016
1 parent 45c911b commit 224b42c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/common/Utilities/Random.cpp
Expand Up @@ -61,6 +61,14 @@ float frand(float min, float max)
return float(GetRng()->Random() * (max - min) + min);
}

Milliseconds randtime(Milliseconds const& min, Milliseconds const& max)
{
long long diff = max.count() - min.count();
ASSERT(diff >= 0);
ASSERT(diff <= (uint32)-1);
return min + Milliseconds(urand(0, diff));
}

uint32 rand32()
{
return GetRng()->BRandom();
Expand Down
4 changes: 4 additions & 0 deletions src/common/Utilities/Random.h
Expand Up @@ -19,6 +19,7 @@
#define Random_h__

#include "Define.h"
#include "Duration.h"
#include <limits>
#include <random>

Expand All @@ -34,6 +35,9 @@ uint32 urandms(uint32 min, uint32 max);
/* Return a random number in the range 0 .. UINT32_MAX. */
uint32 rand32();

/* Return a random time in the range min..max (up to millisecond precision). Only works for values where millisecond difference is a valid uint32. */
Milliseconds randtime(Milliseconds const& min, Milliseconds const& max);

/* Return a random number in the range min..max */
float frand(float min, float max);

Expand Down

0 comments on commit 224b42c

Please sign in to comment.