v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
random-number-generator.h
Go to the documentation of this file.
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_UTILS_RANDOM_NUMBER_GENERATOR_H_
29 #define V8_UTILS_RANDOM_NUMBER_GENERATOR_H_
30 
31 #include "globals.h"
32 
33 namespace v8 {
34 namespace internal {
35 
36 // -----------------------------------------------------------------------------
37 // RandomNumberGenerator
38 //
39 // This class is used to generate a stream of pseudorandom numbers. The class
40 // uses a 48-bit seed, which is modified using a linear congruential formula.
41 // (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)
42 // If two instances of RandomNumberGenerator are created with the same seed, and
43 // the same sequence of method calls is made for each, they will generate and
44 // return identical sequences of numbers.
45 // This class uses (probably) weak entropy by default, but it's sufficient,
46 // because it is the responsibility of the embedder to install an entropy source
47 // using v8::V8::SetEntropySource(), which provides reasonable entropy, see:
48 // https://code.google.com/p/v8/issues/detail?id=2905
49 // This class is neither reentrant nor threadsafe.
50 
51 class RandomNumberGenerator V8_FINAL {
52  public:
53  // EntropySource is used as a callback function when V8 needs a source of
54  // entropy.
55  typedef bool (*EntropySource)(unsigned char* buffer, size_t buflen);
56  static void SetEntropySource(EntropySource entropy_source);
57 
58  RandomNumberGenerator();
59  explicit RandomNumberGenerator(int64_t seed) { SetSeed(seed); }
60 
61  // Returns the next pseudorandom, uniformly distributed int value from this
62  // random number generator's sequence. The general contract of |NextInt()| is
63  // that one int value is pseudorandomly generated and returned.
64  // All 2^32 possible integer values are produced with (approximately) equal
65  // probability.
67  return Next(32);
68  }
69 
70  // Returns a pseudorandom, uniformly distributed int value between 0
71  // (inclusive) and the specified max value (exclusive), drawn from this random
72  // number generator's sequence. The general contract of |NextInt(int)| is that
73  // one int value in the specified range is pseudorandomly generated and
74  // returned. All max possible int values are produced with (approximately)
75  // equal probability.
76  int NextInt(int max) V8_WARN_UNUSED_RESULT;
77 
78  // Returns the next pseudorandom, uniformly distributed boolean value from
79  // this random number generator's sequence. The general contract of
80  // |NextBoolean()| is that one boolean value is pseudorandomly generated and
81  // returned. The values true and false are produced with (approximately) equal
82  // probability.
84  return Next(1) != 0;
85  }
86 
87  // Returns the next pseudorandom, uniformly distributed double value between
88  // 0.0 and 1.0 from this random number generator's sequence.
89  // The general contract of |NextDouble()| is that one double value, chosen
90  // (approximately) uniformly from the range 0.0 (inclusive) to 1.0
91  // (exclusive), is pseudorandomly generated and returned.
92  double NextDouble() V8_WARN_UNUSED_RESULT;
93 
94  // Fills the elements of a specified array of bytes with random numbers.
95  void NextBytes(void* buffer, size_t buflen);
96 
97  private:
98  static const int64_t kMultiplier = V8_2PART_UINT64_C(0x5, deece66d);
99  static const int64_t kAddend = 0xb;
100  static const int64_t kMask = V8_2PART_UINT64_C(0xffff, ffffffff);
101 
102  int Next(int bits) V8_WARN_UNUSED_RESULT;
103  void SetSeed(int64_t seed);
104 
105  int64_t seed_;
106 };
107 
108 } } // namespace v8::internal
109 
110 #endif // V8_UTILS_RANDOM_NUMBER_GENERATOR_H_
#define V8_2PART_UINT64_C(a, b)
Definition: globals.h:226
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:361
bool(* EntropySource)(unsigned char *buffer, size_t length)
Definition: v8.h:4478
V8_INLINE bool NextBool() V8_WARN_UNUSED_RESULT
V8_INLINE int NextInt() V8_WARN_UNUSED_RESULT
#define V8_INLINE
Definition: v8config.h:316