00001 /* $NoKeywords: $ */ 00002 /* 00003 // 00004 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved. 00005 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert 00006 // McNeel & Associates. 00007 // 00008 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. 00009 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF 00010 // MERCHANTABILITY ARE HEREBY DISCLAIMED. 00011 // 00012 // For complete openNURBS copyright information see <http://www.opennurbs.org>. 00013 // 00015 */ 00016 00017 #if !defined(OPENNURBS_RANDOM_NUMBER_INC_) 00018 #define OPENNURBS_RANDOM_NUMBER_INC_ 00019 00020 ON_BEGIN_EXTERNC 00021 00022 struct ON_RANDOM_NUMBER_CONTEXT 00023 { 00024 ON__UINT32 mti; /* mti = 0xFFFFFFFF means mt[] is not initialized */ 00025 ON__UINT32 mt[624]; /* the array for the state vector */ 00026 }; 00027 00028 00029 /* 00030 Description: 00031 Seed a context for on_random_number(). 00032 Parameters: 00033 s - [in] 00034 rand_context - [out] context to seed. 00035 00036 Remarks: 00037 on_random_number_seed() does not use any static memory. 00038 Example: 00039 ON_RAND_CONTEXT rand_context; 00040 00041 ON__UINT seed = 123; 00042 on_random_number_seed( seed, &rand_context ); 00043 00044 ON__UINT32 r1 = on_random_number( &rand_context ); 00045 ON__UINT32 r2 = on_random_number( &rand_context ); 00046 ON__UINT32 r3 = on_random_number( &rand_context ); 00047 */ 00048 void on_random_number_seed( 00049 ON__UINT32 s, 00050 struct ON_RANDOM_NUMBER_CONTEXT* rand_context 00051 ); 00052 00053 /* 00054 Description: 00055 Get a random number. 00056 Parameters: 00057 rand_context - [in/out] 00058 random number context. The first time rand_context is 00059 used it must be either initialized by calling on_random_number_seed() 00060 or rand_context->mti must be set to 0xFFFFFFFF. Otherwise do not 00061 modify randcontext between calls to on_random_number. 00062 Returns: 00063 A random number. 00064 Remarks: 00065 on_random_number() does not use any static memory. 00066 Example: 00067 ON_RAND_CONTEXT rand_context; 00068 00069 ON__UINT seed = 123; 00070 on_random_number_seed( seed, &rand_context ); 00071 00072 ON__UINT32 r1 = on_random_number( &rand_context ); 00073 ON__UINT32 r2 = on_random_number( &rand_context ); 00074 ON__UINT32 r3 = on_random_number( &rand_context ); 00075 */ 00076 ON__UINT32 on_random_number( 00077 struct ON_RANDOM_NUMBER_CONTEXT* rand_context 00078 ); 00079 00080 00081 /* 00082 Description: 00083 Seed the random number generator used by on_rand(). 00084 Parameters: 00085 s - [in] 00086 Remarks: 00087 on_srand() is not thread safe. It used static global memory 00088 that is modified by on_srand() and on_rand(). 00089 */ 00090 void on_srand(ON__UINT32 s); 00091 00092 /* 00093 Description: 00094 Get a random number. 00095 Returns: 00096 A random number. 00097 Remarks: 00098 on_rand() is not thread safe. It used static global memory 00099 that is modified by on_srand() and on_rand(). 00100 */ 00101 ON__UINT32 on_rand(void); 00102 00103 00104 ON_END_EXTERNC 00105 00106 00107 #if defined(ON_CPLUSPLUS) 00108 00109 class ON_CLASS ON_RandomNumberGenerator 00110 { 00111 public: 00112 ON_RandomNumberGenerator(); 00113 00114 /* 00115 Description: 00116 Seed the random number generator. 00117 Parameters: 00118 s - [in] 00119 */ 00120 void Seed( ON__UINT32 s ); 00121 00122 /* 00123 Returns: 00124 32 bit unsigned random number [0,0xFFFFFFFF] [0,4294967295] 00125 */ 00126 ON__UINT32 RandomNumber(); 00127 00128 /* 00129 Returns: 00130 double in the interval [0.0 and 1.0] 00131 */ 00132 double RandomDouble(); 00133 00134 /* 00135 Returns: 00136 double in the interval [t0,t1] 00137 */ 00138 double RandomDouble(double t0, double t1); 00139 00140 /* 00141 Description: 00142 Perform a random permuation on an array. 00143 Parameters: 00144 base - [in/out] 00145 Array of element to permute 00146 nel - [in] 00147 number of elements in the array. 00148 sizeof_element 00149 size of an element in bytes. 00150 */ 00151 void RandomPermutation(void* base, size_t nel, size_t sizeof_element ); 00152 00153 private: 00154 struct ON_RANDOM_NUMBER_CONTEXT m_rand_context; 00155 }; 00156 00157 #endif 00158 00159 00160 #endif