Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_RANDOM_H_
6#define RUNTIME_VM_RANDOM_H_
7
8#include <atomic>
9
10#include "vm/allocation.h"
11#include "vm/flags.h"
12#include "vm/globals.h"
13
14namespace dart {
15
16DECLARE_FLAG(uint64_t, random_seed);
17
18class Random {
19 public:
20 Random();
21 // Seed must be non-zero.
22 explicit Random(uint64_t seed);
23 ~Random();
24
25 uint32_t NextUInt32();
26 uint64_t NextUInt64() {
27 return (static_cast<uint64_t>(NextUInt32()) << 32) |
28 static_cast<uint64_t>(NextUInt32());
29 }
30
31 // Returns a random number that's a valid JS integer.
32 //
33 // All IDs that can be returned over the service protocol should be
34 // representable as JS integers and should be generated using this method.
35 //
36 // See https://github.com/dart-lang/sdk/issues/53081.
37 uint64_t NextJSInt() {
38 // Number.MAX_SAFE_INTEGER (2 ^ 53 - 1)
39 const uint64_t kMaxJsInt = 0x1FFFFFFFFFFFFF;
40 return NextUInt64() & kMaxJsInt;
41 }
42
43 static uint64_t GlobalNextUInt64();
44 static void Init();
45 static void Cleanup();
46
47 // Generates a uniform random variable in the range [0,1].
48 double NextDouble();
49
50 private:
51 uint64_t NextState();
52 void Initialize(uint64_t seed);
53
54 std::atomic<uint64_t> _state;
55
57};
58
59} // namespace dart
60
61#endif // RUNTIME_VM_RANDOM_H_
static uint64_t GlobalNextUInt64()
Definition random.cc:95
static void Cleanup()
Definition random.cc:88
uint64_t NextUInt64()
Definition random.h:26
static void Init()
Definition random.cc:81
double NextDouble()
Definition random.cc:100
uint32_t NextUInt32()
Definition random.cc:73
uint64_t NextJSInt()
Definition random.h:37
#define DECLARE_FLAG(type, name)
Definition flags.h:14
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581