|
|
|
|
|
by torotonnato
1531 days ago
|
|
You can apply the core ideas of Hacker's Delight to floats too. Your example problem, taken from [0]: float frand( int *seed )
{
union
{
float fres;
unsigned int ires;
};
seed[0] *= 16807;
ires = ((((unsigned int)seed[0])>>9 ) | 0x3f800000);
return fres - 1.0f;
}
You basically assemble a float building a random mantissa and
assigning the right exponent and sign bits.It's not portable, but it's a very neat trick! [0] https://iquilezles.org/www/articles/sfrand/sfrand.htm* |
|