| 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 "vm/allocation.h" |
| 9 | #include "vm/flags.h" |
| 10 | #include "vm/globals.h" |
| 11 | |
| 12 | namespace dart { |
| 13 | |
| 14 | DECLARE_FLAG(uint64_t, random_seed); |
| 15 | |
| 16 | class Random { |
| 17 | public: |
| 18 | Random(); |
| 19 | // Seed must be non-zero. |
| 20 | explicit Random(uint64_t seed); |
| 21 | ~Random(); |
| 22 | |
| 23 | uint32_t NextUInt32(); |
| 24 | uint64_t NextUInt64() { |
| 25 | return (static_cast<uint64_t>(NextUInt32()) << 32) | |
| 26 | static_cast<uint64_t>(NextUInt32()); |
| 27 | } |
| 28 | |
| 29 | private: |
| 30 | void NextState(); |
| 31 | void Initialize(uint64_t seed); |
| 32 | |
| 33 | uint64_t _state; |
| 34 | |
| 35 | DISALLOW_COPY_AND_ASSIGN(Random); |
| 36 | }; |
| 37 | |
| 38 | } // namespace dart |
| 39 | |
| 40 | #endif // RUNTIME_VM_RANDOM_H_ |
| 41 | |