Also, reimplement random_uint32() to make fewer calls to rand().
}
}
+uint8_t
+random_uint8(void)
+{
+ random_init();
+ return rand();
+}
+
+uint16_t
+random_uint16(void)
+{
+ if (RAND_MAX >= UINT16_MAX) {
+ random_init();
+ return rand();
+ } else {
+ uint16_t x;
+ random_bytes(&x, sizeof x);
+ return x;
+ }
+}
+
uint32_t
random_uint32(void)
{
- uint32_t x;
- random_bytes(&x, sizeof x);
- return x;
+ if (RAND_MAX >= UINT32_MAX) {
+ random_init();
+ return rand();
+ } else if (RAND_MAX == INT32_MAX) {
+ random_init();
+ return rand() | ((rand() & 1u) << 31);
+ } else {
+ uint32_t x;
+ random_bytes(&x, sizeof x);
+ return x;
+ }
}
int
#include <stdint.h>
void random_bytes(void *, size_t);
+uint8_t random_uint8(void);
+uint16_t random_uint16(void);
uint32_t random_uint32(void);
int random_range(int max);