OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 // ----------------------------------------------------------------------------- | 36 // ----------------------------------------------------------------------------- |
37 // RandomNumberGenerator | 37 // RandomNumberGenerator |
38 // | 38 // |
39 // This class is used to generate a stream of pseudorandom numbers. The class | 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. | 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.) | 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 | 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 | 43 // the same sequence of method calls is made for each, they will generate and |
44 // return identical sequences of numbers. | 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 |
45 // This class is neither reentrant nor threadsafe. | 49 // This class is neither reentrant nor threadsafe. |
46 | 50 |
47 class RandomNumberGenerator V8_FINAL { | 51 class RandomNumberGenerator V8_FINAL { |
48 public: | 52 public: |
49 // EntropySource is used as a callback function when V8 needs a source of | 53 // EntropySource is used as a callback function when V8 needs a source of |
50 // entropy. | 54 // entropy. |
51 typedef bool (*EntropySource)(unsigned char* buffer, size_t buflen); | 55 typedef bool (*EntropySource)(unsigned char* buffer, size_t buflen); |
52 static void SetEntropySource(EntropySource entropy_source); | 56 static void SetEntropySource(EntropySource entropy_source); |
53 | 57 |
54 RandomNumberGenerator(); | 58 RandomNumberGenerator(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 101 |
98 int Next(int bits) V8_WARN_UNUSED_RESULT; | 102 int Next(int bits) V8_WARN_UNUSED_RESULT; |
99 void SetSeed(int64_t seed); | 103 void SetSeed(int64_t seed); |
100 | 104 |
101 int64_t seed_; | 105 int64_t seed_; |
102 }; | 106 }; |
103 | 107 |
104 } } // namespace v8::internal | 108 } } // namespace v8::internal |
105 | 109 |
106 #endif // V8_UTILS_RANDOM_NUMBER_GENERATOR_H_ | 110 #endif // V8_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
OLD | NEW |