Hello!
I'm working on some procedural generation code, and I need to convert some of my C logic into Nim.
I have a function that returns a hash as a uint32. I want to use this to generate a float between 0.0 and 254.99999
Here's a minimal example of how I'd do this in C:
#include <stdio.h>
#include <limits.h>
#include <assert.h>
int main(int argc, char *argv[])
{
unsigned int hash = 2392244132; // in real life, output by hash function
float a = 254.9999; // Maxiumim value I want, min is 0
float x = ((float)hash/(float)(UINT_MAX)) * a;
assert(x == (float)142.031815);
return 0;
}
How would I achieve this in Nim?
If I understand your desire as seeding the generation of a random float this is what I would do
import random
proc seededFloat(seed: int32): float32 =
randomize(seed)
rand(254.99999f32)
echo seededFloat(314159265)