vendredi 12 janvier 2018

Random number generator PCG library. How to generate float numbers set within a range. Working example in c language

For example, we can use internal rand(), but it is the worst choice:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

float randoms(float min, float max)
{
    return (float)(rand())/RAND_MAX*(max - min) + min;
}

int main()
{
    srand((unsigned int)time(0));
    printf("%f\n",randoms(-100.001, 100.001));
    return 0;
}

I've searched, but not found any working example of PCG library for float numbers.

In the answer I would like to share my own experience of using PCG random library to generate float numbers within a fixed range. Previously I've used arc4random library for this goal, but PCG is simpler and has no complicated dependencies.




Aucun commentaire:

Enregistrer un commentaire