opennurbs_rand.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 #if !defined(OPENNURBS_RANDOM_NUMBER_INC_)
18 #define OPENNURBS_RANDOM_NUMBER_INC_
19 
20 ON_BEGIN_EXTERNC
21 
23 {
24  ON__UINT32 mti; /* mti = 0xFFFFFFFF means mt[] is not initialized */
25  ON__UINT32 mt[624]; /* the array for the state vector */
26 };
27 
28 
29 /*
30 Description:
31  Seed a context for on_random_number().
32 Parameters:
33  s - [in]
34  rand_context - [out] context to seed.
35 
36 Remarks:
37  on_random_number_seed() does not use any static memory.
38 Example:
39  ON_RAND_CONTEXT rand_context;
40 
41  ON__UINT seed = 123;
42  on_random_number_seed( seed, &rand_context );
43 
44  ON__UINT32 r1 = on_random_number( &rand_context );
45  ON__UINT32 r2 = on_random_number( &rand_context );
46  ON__UINT32 r3 = on_random_number( &rand_context );
47 */
48 void on_random_number_seed(
49  ON__UINT32 s,
50  struct ON_RANDOM_NUMBER_CONTEXT* rand_context
51  );
52 
53 /*
54 Description:
55  Get a random number.
56 Parameters:
57  rand_context - [in/out]
58  random number context. The first time rand_context is
59  used it must be either initialized by calling on_random_number_seed()
60  or rand_context->mti must be set to 0xFFFFFFFF. Otherwise do not
61  modify randcontext between calls to on_random_number.
62 Returns:
63  A random number.
64 Remarks:
65  on_random_number() does not use any static memory.
66 Example:
67  ON_RAND_CONTEXT rand_context;
68 
69  ON__UINT seed = 123;
70  on_random_number_seed( seed, &rand_context );
71 
72  ON__UINT32 r1 = on_random_number( &rand_context );
73  ON__UINT32 r2 = on_random_number( &rand_context );
74  ON__UINT32 r3 = on_random_number( &rand_context );
75 */
76 ON__UINT32 on_random_number(
77  struct ON_RANDOM_NUMBER_CONTEXT* rand_context
78  );
79 
80 
81 /*
82 Description:
83  Seed the random number generator used by on_rand().
84 Parameters:
85  s - [in]
86 Remarks:
87  on_srand() is not thread safe. It used static global memory
88  that is modified by on_srand() and on_rand().
89 */
90 void on_srand(ON__UINT32 s);
91 
92 /*
93 Description:
94  Get a random number.
95 Returns:
96  A random number.
97 Remarks:
98  on_rand() is not thread safe. It used static global memory
99  that is modified by on_srand() and on_rand().
100 */
101 ON__UINT32 on_rand(void);
102 
103 
104 ON_END_EXTERNC
105 
106 
107 #if defined(ON_CPLUSPLUS)
108 
109 class ON_CLASS ON_RandomNumberGenerator
110 {
111 public:
112  ON_RandomNumberGenerator();
113 
114  /*
115  Description:
116  Seed the random number generator.
117  Parameters:
118  s - [in]
119  */
120  void Seed( ON__UINT32 s );
121 
122  /*
123  Description:
124  Seed the random number generator in a way that cannot be reproduced.
125  */
126  void Seed();
127 
128  /*
129  Returns:
130  32 bit unsigned random number [0,0xFFFFFFFF] [0,4294967295]
131  */
132  ON__UINT32 RandomNumber();
133 
134  /*
135  Returns:
136  double in the interval [0.0 and 1.0]
137  */
138  double RandomDouble();
139 
140  /*
141  Returns:
142  double in the interval [t0,t1]
143  */
144  double RandomDouble(double t0, double t1);
145 
146  /*
147  Description:
148  Perform a random permuation on an array.
149  Parameters:
150  base - [in/out]
151  Array of element to permute
152  nel - [in]
153  number of elements in the array.
154  sizeof_element
155  size of an element in bytes.
156  */
157  void RandomPermutation(void* base, size_t nel, size_t sizeof_element );
158 
159 private:
160  struct ON_RANDOM_NUMBER_CONTEXT m_rand_context;
161 };
162 
163 #endif
164 
165 
166 #endif
ON__UINT32 mti
Definition: opennurbs_rand.h:24
ON__UINT32 mt[624]
Definition: opennurbs_rand.h:25
Definition: opennurbs_rand.h:22