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.
Public Member Functions | Private Attributes | List of all members
qrtplib::RTPRandomURandom Class Reference

#include <rtprandomurandom.h>

+ Inheritance diagram for qrtplib::RTPRandomURandom:
+ Collaboration diagram for qrtplib::RTPRandomURandom:

Public Member Functions

 RTPRandomURandom ()
 
 ~RTPRandomURandom ()
 
int Init ()
 
uint8_t GetRandom8 ()
 
uint16_t GetRandom16 ()
 
uint32_t GetRandom32 ()
 
double GetRandomDouble ()
 
- Public Member Functions inherited from qrtplib::RTPRandom
 RTPRandom ()
 
virtual ~RTPRandom ()
 
uint32_t PickSeed ()
 

Private Attributes

FILE * device
 

Additional Inherited Members

- Static Public Member Functions inherited from qrtplib::RTPRandom
static RTPRandomCreateDefaultRandomNumberGenerator ()
 

Detailed Description

A random number generator which uses bytes delivered by the /dev/urandom device.

Definition at line 51 of file rtprandomurandom.h.

Constructor & Destructor Documentation

◆ RTPRandomURandom()

qrtplib::RTPRandomURandom::RTPRandomURandom ( )

Definition at line 41 of file rtprandomurandom.cpp.

References device.

42 {
43  device = 0;
44 }

◆ ~RTPRandomURandom()

qrtplib::RTPRandomURandom::~RTPRandomURandom ( )

Definition at line 46 of file rtprandomurandom.cpp.

References device.

47 {
48  if (device) {
49  fclose(device);
50  }
51 }

Member Function Documentation

◆ GetRandom16()

uint16_t qrtplib::RTPRandomURandom::GetRandom16 ( )
virtual

Returns a random sixteen bit value.

Implements qrtplib::RTPRandom.

Definition at line 87 of file rtprandomurandom.cpp.

References device.

88 {
89  if (!device)
90  {
91  qWarning("RTPRandomURandom::GetRandom16: no device");
92  return 0;
93  }
94 
95  uint16_t value;
96 
97  if (fread(&value, 1, sizeof(uint16_t), device) != sizeof(uint16_t))
98  {
99  qWarning("RTPRandomURandom::GetRandom16: cannot read unsigned 16 bit value from device");
100  return 0;
101  }
102 
103  return value;
104 }
unsigned short uint16_t
Definition: rtptypes_win.h:44

◆ GetRandom32()

uint32_t qrtplib::RTPRandomURandom::GetRandom32 ( )
virtual

Returns a random thirty-two bit value.

Implements qrtplib::RTPRandom.

Definition at line 106 of file rtprandomurandom.cpp.

References device.

107 {
108  if (!device)
109  {
110  qWarning("RTPRandomURandom::GetRandom32: no device");
111  return 0;
112  }
113 
114  uint32_t value;
115 
116  if (fread(&value, 1, sizeof(uint32_t), device) != sizeof(uint32_t))
117  {
118  qWarning("RTPRandomURandom::GetRandom32: cannot read unsigned 32 bit value from device");
119  return 0;
120  }
121 
122  return value;
123 }
unsigned int uint32_t
Definition: rtptypes_win.h:46

◆ GetRandom8()

uint8_t qrtplib::RTPRandomURandom::GetRandom8 ( )
virtual

Returns a random eight bit value.

Implements qrtplib::RTPRandom.

Definition at line 68 of file rtprandomurandom.cpp.

References device.

69 {
70  if (!device)
71  {
72  qWarning("RTPRandomURandom::GetRandom8: no device");
73  return 0;
74  }
75 
76  uint8_t value;
77 
78  if (fread(&value, 1, sizeof(uint8_t), device) != sizeof(uint8_t))
79  {
80  qWarning("RTPRandomURandom::GetRandom8: cannot read unsigned 8 bit value from device");
81  return 0;
82  }
83 
84  return value;
85 }
unsigned char uint8_t
Definition: rtptypes_win.h:42

◆ GetRandomDouble()

double qrtplib::RTPRandomURandom::GetRandomDouble ( )
virtual

Returns a random number between $0.0$ and $1.0$.

Implements qrtplib::RTPRandom.

Definition at line 125 of file rtprandomurandom.cpp.

References device, and RTPRANDOM_2POWMIN63.

126 {
127  if (!device)
128  {
129  qWarning("RTPRandomURandom::GetRandomDouble: no device");
130  return 0;
131  }
132 
133  uint64_t value;
134 
135  if (fread(&value, 1, sizeof(uint64_t), device) != sizeof(uint64_t))
136  {
137  qWarning("RTPRandomURandom::GetRandomDouble: cannot read unsigned 64 bit value from device");
138  return 0;
139  }
140 
141  value &= 0x7fffffffffffffffULL;
142  int64_t value2 = (int64_t) value;
143  double x = RTPRANDOM_2POWMIN63 * (double) value2;
144 
145  return x;
146 }
#define RTPRANDOM_2POWMIN63
Definition: rtprandom.h:47
__int64 int64_t
Definition: rtptypes_win.h:47
unsigned __int64 uint64_t
Definition: rtptypes_win.h:48

◆ Init()

int qrtplib::RTPRandomURandom::Init ( )

Initialize the random number generator.

Definition at line 53 of file rtprandomurandom.cpp.

References device, ERR_RTP_RTPRANDOMURANDOM_ALREADYOPEN, and ERR_RTP_RTPRANDOMURANDOM_CANTOPEN.

Referenced by qrtplib::RTPRandom::CreateDefaultRandomNumberGenerator().

54 {
55  if (device) {
57  }
58 
59  device = fopen("/dev/urandom", "rb");
60 
61  if (device == 0) {
63  }
64 
65  return 0;
66 }
#define ERR_RTP_RTPRANDOMURANDOM_CANTOPEN
Definition: rtperrors.h:188
#define ERR_RTP_RTPRANDOMURANDOM_ALREADYOPEN
Definition: rtperrors.h:189
+ Here is the caller graph for this function:

Member Data Documentation

◆ device

FILE* qrtplib::RTPRandomURandom::device
private

The documentation for this class was generated from the following files: