SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
rtprandomrands.cpp
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2017 Jori Liesenborgs
5 
6  Contact: jori.liesenborgs@gmail.com
7 
8  This library was developed at the Expertise Centre for Digital Media
9  (http://www.edm.uhasselt.be), a research center of the Hasselt University
10  (http://www.uhasselt.be). The library is based upon work done for
11  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13  Permission is hereby granted, free of charge, to any person obtaining a
14  copy of this software and associated documentation files (the "Software"),
15  to deal in the Software without restriction, including without limitation
16  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the
18  Software is furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included
21  in all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  IN THE SOFTWARE.
30 
31  */
32 
33 #include "rtpconfig.h"
34 #ifdef RTP_HAVE_RAND_S
35 #define _CRT_RAND_S
36 #endif // RTP_HAVE_RAND_S
37 
38 #include "rtprandomrands.h"
39 #include "rtperrors.h"
40 #include <stdlib.h>
41 #include <math.h>
42 
43 // If compiling on VC 8 or later for full Windows, we'll attempt to use rand_s,
44 // which generates better random numbers. However, its only supported on Windows XP,
45 // Windows Server 2003, and later, so we'll do a run-time check to see if we can
46 // use it (it won't work on Windows 2000 SP4 for example).
47 
48 namespace qrtplib
49 {
50 
51 #ifndef RTP_HAVE_RAND_S
52 
54 {
55  initialized = false;
56 }
57 
59 {
60 }
61 
63 {
65 }
66 
68 {
69  return 0;
70 }
71 
73 {
74  return 0;
75 }
76 
78 {
79  return 0;
80 }
81 
83 {
84  return 0;
85 }
86 
87 #else
88 
90 {
91  initialized = false;
92 }
93 
95 {
96 }
97 
99 {
100  if (initialized) // doesn't matter then
101  return 0;
102 
103  HMODULE hAdvApi32 = LoadLibrary(TEXT("ADVAPI32.DLL"));
104  if(hAdvApi32 != NULL)
105  {
106  if(NULL != GetProcAddress( hAdvApi32, "SystemFunction036" )) // RtlGenRandom
107  {
108  initialized = true;
109  }
110  FreeLibrary(hAdvApi32);
111  hAdvApi32 = NULL;
112  }
113 
114  if (!initialized)
116  return 0;
117 }
118 
119 // rand_s gives a number between 0 and UINT_MAX. We'll assume that UINT_MAX is at least 8 bits
120 
122 {
123  if (!initialized)
124  return 0;
125 
126  unsigned int r;
127 
128  rand_s(&r);
129 
130  return (uint8_t)(r&0xff);
131 }
132 
134 {
135  if (!initialized)
136  return 0;
137 
138  unsigned int r;
139  int shift = 0;
140  uint16_t x = 0;
141 
142  for (int i = 0; i < 2; i++, shift += 8)
143  {
144  rand_s(&r);
145 
146  x ^= ((uint16_t)r << shift);
147  }
148 
149  return x;
150 }
151 
153 {
154  if (!initialized)
155  return 0;
156 
157  unsigned int r;
158  int shift = 0;
159  uint32_t x = 0;
160 
161  for (int i = 0; i < 4; i++, shift += 8)
162  {
163  rand_s(&r);
164 
165  x ^= ((uint32_t)r << shift);
166  }
167 
168  return x;
169 }
170 
172 {
173  if (!initialized)
174  return 0;
175 
176  unsigned int r;
177  int shift = 0;
178  uint64_t x = 0;
179 
180  for (int i = 0; i < 8; i++, shift += 8)
181  {
182  rand_s(&r);
183 
184  x ^= ((uint64_t)r << shift);
185  }
186 
187  x &= 0x7fffffffffffffffULL;
188 
189  int64_t x2 = (int64_t)x;
190  return RTPRANDOM_2POWMIN63*(double)x2;
191 }
192 
193 #endif // RTP_HAVE_RAND_S
194 
195 }
196  // end namespace
197 
#define RTPRANDOM_2POWMIN63
Definition: rtprandom.h:47
__int64 int64_t
Definition: rtptypes_win.h:47
unsigned int uint32_t
Definition: rtptypes_win.h:46
unsigned char uint8_t
Definition: rtptypes_win.h:42
unsigned short uint16_t
Definition: rtptypes_win.h:44
int32_t i
Definition: decimators.h:244
#define ERR_RTP_RTPRANDOMRANDS_NOTSUPPORTED
Definition: rtperrors.h:190
unsigned __int64 uint64_t
Definition: rtptypes_win.h:48