Skip to content

Instantly share code, notes, and snippets.

@zhaoyk
Forked from cloudwu/ffr.c
Created December 22, 2013 08:06
Show Gist options
  • Save zhaoyk/8079623 to your computer and use it in GitHub Desktop.
Save zhaoyk/8079623 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdlib.h>
// assert(RAND_MAX >= 0x7fff)
float
random_0_to_1() {
union {
uint32_t d;
float f;
} u;
u.d = (((uint32_t)rand() & 0x7fff) << 8) | 0x3f800000;
return u.f - 1.0f;
}
float
random_minus1_to_1() {
union {
uint32_t d;
float f;
} u;
u.d = (((uint32_t)rand() & 0x7fff) << 8) | 0x40000000;
return u.f - 3.0f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment