Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
random.h
00001 ///\file 00002 00003 /****************************************************************************** 00004 The MIT License(MIT) 00005 00006 Embedded Template Library. 00007 https://github.com/ETLCPP/etl 00008 http://www.etlcpp.com 00009 00010 Copyright(c) 2017 jwellbelove 00011 00012 Permission is hereby granted, free of charge, to any person obtaining a copy 00013 of this software and associated documentation files(the "Software"), to deal 00014 in the Software without restriction, including without limitation the rights 00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell 00016 copies of the Software, and to permit persons to whom the Software is 00017 furnished to do so, subject to the following conditions : 00018 00019 The above copyright notice and this permission notice shall be included in all 00020 copies or substantial portions of the Software. 00021 00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE 00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00028 SOFTWARE. 00029 ******************************************************************************/ 00030 00031 #ifndef __ETL_RANDOM__ 00032 #define __ETL_RANDOM__ 00033 00034 #include <stdint.h> 00035 00036 #include "platform.h " 00037 00038 namespace etl 00039 { 00040 //*************************************************************************** 00041 /// The base for all 32 bit random number generators. 00042 //*************************************************************************** 00043 class random 00044 { 00045 public: 00046 00047 virtual ~random() 00048 { 00049 } 00050 00051 virtual void initialise(uint32_t seed) = 0; 00052 virtual uint32_t operator()() = 0; 00053 virtual uint32_t range(uint32_t low, uint32_t high) = 0; 00054 }; 00055 00056 //*************************************************************************** 00057 /// A 32 bit random number generator. 00058 /// Uses a 128 bit XOR shift algorithm. 00059 /// https://en.wikipedia.org/wiki/Xorshift 00060 //*************************************************************************** 00061 class random_xorshift : public random 00062 { 00063 public: 00064 00065 random_xorshift(); 00066 explicit random_xorshift(uint32_t seed); 00067 void initialise(uint32_t seed); 00068 uint32_t operator()(); 00069 uint32_t range(uint32_t low, uint32_t high); 00070 00071 private: 00072 00073 uint32_t state[4]; 00074 }; 00075 00076 //*************************************************************************** 00077 /// A 32 bit random number generator. 00078 /// Uses a linear congruential generator. 00079 /// https://cs.adelaide.edu.au/~paulc/teaching/montecarlo/node107.html 00080 //*************************************************************************** 00081 class random_lcg : public random 00082 { 00083 public: 00084 00085 random_lcg(); 00086 explicit random_lcg(uint32_t seed); 00087 void initialise(uint32_t seed); 00088 uint32_t operator()(); 00089 uint32_t range(uint32_t low, uint32_t high); 00090 00091 private: 00092 00093 static const uint32_t a = 40014; 00094 static const uint32_t m = 2147483563; 00095 00096 uint32_t value; 00097 }; 00098 00099 //*************************************************************************** 00100 /// A 32 bit random number generator. 00101 /// Uses a combined linear congruential generator. 00102 /// https://cs.adelaide.edu.au/~paulc/teaching/montecarlo/node107.html 00103 //*************************************************************************** 00104 class random_clcg : public random 00105 { 00106 public: 00107 00108 random_clcg(); 00109 explicit random_clcg(uint32_t seed); 00110 void initialise(uint32_t seed); 00111 uint32_t operator()(); 00112 uint32_t range(uint32_t low, uint32_t high); 00113 00114 private: 00115 00116 static const uint32_t a1 = 40014; 00117 static const uint32_t m1 = 2147483563; 00118 00119 static const uint32_t a2 = 40692; 00120 static const uint32_t m2 = 2147483399; 00121 00122 uint32_t value1; 00123 uint32_t value2; 00124 }; 00125 00126 //*************************************************************************** 00127 /// A 32 bit random number generator. 00128 /// Uses a linear shift feedback register. 00129 /// https://en.wikipedia.org/wiki/Linear-feedback_shift_register 00130 //*************************************************************************** 00131 class random_lsfr : public random 00132 { 00133 public: 00134 00135 random_lsfr(); 00136 explicit random_lsfr(uint32_t seed); 00137 void initialise(uint32_t seed); 00138 uint32_t operator()(); 00139 uint32_t range(uint32_t low, uint32_t high); 00140 00141 private: 00142 00143 uint32_t value; 00144 }; 00145 00146 //*************************************************************************** 00147 /// A 32 bit random number generator. 00148 /// Uses a multiply with carry calculation. 00149 //*************************************************************************** 00150 class random_mwc : public random 00151 { 00152 public: 00153 00154 random_mwc(); 00155 explicit random_mwc(uint32_t seed); 00156 void initialise(uint32_t seed); 00157 uint32_t operator()(); 00158 uint32_t range(uint32_t low, uint32_t high); 00159 00160 private: 00161 00162 uint32_t value1; 00163 uint32_t value2; 00164 }; 00165 } 00166 00167 #endif 00168
Generated on Tue Jul 12 2022 14:05:43 by
