1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef TBB_examples_fast_random_H
18 #define TBB_examples_fast_random_H
19 
20 #include <cstddef>
21 
22 namespace utility {
23 //------------------------------------------------------------------------
24 // FastRandom
25 //------------------------------------------------------------------------
26 
27 namespace internal {
28 std::size_t GetPrime(std::size_t seed);
29 }
30 
31 //! A fast random number generator.
32 /** Uses linear congruential method. */
33 class FastRandom {
34     std::size_t x, a;
35 
36 public:
37     //! Get a random number.
get()38     unsigned short get() {
39         return get(x);
40     }
41     //! Get a random number for the given seed; update the seed for next use.
get(std::size_t & seed)42     unsigned short get(std::size_t& seed) {
43         unsigned short r = (unsigned short)(seed >> 16);
44         seed = seed * a + 1;
45         return r;
46     }
47     //! Construct a random number generator.
FastRandom(std::size_t seed)48     FastRandom(std::size_t seed) {
49         x = seed * internal::GetPrime(seed);
50         a = internal::GetPrime(x);
51     }
52 };
53 } // namespace utility
54 
55 namespace utility {
56 namespace internal {
57 //! Table of primes used by fast random-number generator (FastRandom).
58 static const unsigned Primes[] = {
59     0x9e3779b1, 0xffe6cc59, 0x2109f6dd, 0x43977ab5, 0xba5703f5, 0xb495a877, 0xe1626741, 0x79695e6b,
60     0xbc98c09f, 0xd5bee2b3, 0x287488f9, 0x3af18231, 0x9677cd4d, 0xbe3a6929, 0xadc6a877, 0xdcf0674b,
61     0xbe4d6fe9, 0x5f15e201, 0x99afc3fd, 0xf3f16801, 0xe222cfff, 0x24ba5fdb, 0x0620452d, 0x79f149e3,
62     0xc8b93f49, 0x972702cd, 0xb07dd827, 0x6c97d5ed, 0x085a3d61, 0x46eb5ea7, 0x3d9910ed, 0x2e687b5b,
63     0x29609227, 0x6eb081f1, 0x0954c4e1, 0x9d114db9, 0x542acfa9, 0xb3e6bd7b, 0x0742d917, 0xe9f3ffa7,
64     0x54581edb, 0xf2480f45, 0x0bb9288f, 0xef1affc7, 0x85fa0ca7, 0x3ccc14db, 0xe6baf34b, 0x343377f7,
65     0x5ca19031, 0xe6d9293b, 0xf0a9f391, 0x5d2e980b, 0xfc411073, 0xc3749363, 0xb892d829, 0x3549366b,
66     0x629750ad, 0xb98294e5, 0x892d9483, 0xc235baf3, 0x3d2402a3, 0x6bdef3c9, 0xbec333cd, 0x40c9520f
67 };
GetPrime(std::size_t seed)68 std::size_t GetPrime(std::size_t seed) {
69     return Primes[seed % (sizeof(Primes) / sizeof(Primes[0]))];
70 }
71 } // namespace internal
72 } // namespace utility
73 
74 #endif /* TBB_examples_fast_random_H */
75