Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | List of all members
dart::Random Class Reference

#include <random.h>

Public Member Functions

 Random ()
 
 Random (uint64_t seed)
 
 ~Random ()
 
uint32_t NextUInt32 ()
 
uint64_t NextUInt64 ()
 
uint64_t NextJSInt ()
 
double NextDouble ()
 

Static Public Member Functions

static uint64_t GlobalNextUInt64 ()
 
static void Init ()
 
static void Cleanup ()
 

Detailed Description

Definition at line 18 of file random.h.

Constructor & Destructor Documentation

◆ Random() [1/2]

dart::Random::Random ( )

Definition at line 17 of file random.cc.

17 {
18 uint64_t seed = FLAG_random_seed;
19 if (seed == 0) {
21 if (callback != nullptr) {
22 if (!callback(reinterpret_cast<uint8_t*>(&seed), sizeof(seed))) {
23 // Callback failed. Reset the seed to 0.
24 seed = 0;
25 }
26 }
27 }
28 if (seed == 0) {
29 // We did not get a seed so far. As a fallback we do use the current time.
31 }
32 Initialize(seed);
33}
static Dart_EntropySource entropy_source_callback()
Definition: dart.h:135
static int64_t GetCurrentTimeMicros()
bool(* Dart_EntropySource)(uint8_t *buffer, intptr_t length)
Definition: dart_api.h:822
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback

◆ Random() [2/2]

dart::Random::Random ( uint64_t  seed)
explicit

Definition at line 45 of file random.cc.

45 {
46 Initialize(seed);
47}

◆ ~Random()

dart::Random::~Random ( )

Definition at line 49 of file random.cc.

49 {
50 // Nothing to be done here.
51}

Member Function Documentation

◆ Cleanup()

void dart::Random::Cleanup ( )
static

Definition at line 88 of file random.cc.

88 {
90 global_random_mutex = nullptr;
91 delete global_random;
92 global_random = nullptr;
93}
static Mutex * global_random_mutex
Definition: random.cc:79
static Random * global_random
Definition: random.cc:78

◆ GlobalNextUInt64()

uint64_t dart::Random::GlobalNextUInt64 ( )
static

Definition at line 95 of file random.cc.

95 {
96 MutexLocker locker(global_random_mutex);
97 return global_random->NextUInt64();
98}
uint64_t NextUInt64()
Definition: random.h:26

◆ Init()

void dart::Random::Init ( )
static

Definition at line 81 of file random.cc.

81 {
82 ASSERT(global_random_mutex == nullptr);
83 global_random_mutex = new Mutex(NOT_IN_PRODUCT("global_random_mutex"));
84 ASSERT(global_random == nullptr);
85 global_random = new Random();
86}
#define ASSERT(E)
NOT_IN_PRODUCT(LibraryPtr ReloadTestScript(const char *script))

◆ NextDouble()

double dart::Random::NextDouble ( )

Definition at line 100 of file random.cc.

100 {
101 uint64_t mantissa = NextUInt64() & 0xFFFFFFFFFFFFF;
102 // The exponent value 0 in biased form.
103 const uint64_t exp = 1023;
104 return bit_cast<double>(exp << 52 | mantissa) - 1.0;
105}

◆ NextJSInt()

uint64_t dart::Random::NextJSInt ( )
inline

Definition at line 37 of file random.h.

37 {
38 // Number.MAX_SAFE_INTEGER (2 ^ 53 - 1)
39 const uint64_t kMaxJsInt = 0x1FFFFFFFFFFFFF;
40 return NextUInt64() & kMaxJsInt;
41 }

◆ NextUInt32()

uint32_t dart::Random::NextUInt32 ( )

Definition at line 73 of file random.cc.

73 {
74 const uint64_t MASK_32 = 0xffffffff;
75 return static_cast<uint32_t>(NextState() & MASK_32);
76}

◆ NextUInt64()

uint64_t dart::Random::NextUInt64 ( )
inline

Definition at line 26 of file random.h.

26 {
27 return (static_cast<uint64_t>(NextUInt32()) << 32) |
28 static_cast<uint64_t>(NextUInt32());
29 }
uint32_t NextUInt32()
Definition: random.cc:73

The documentation for this class was generated from the following files: