I know this is a rather simple question, but I'm just not too good at maths.
I know how to generate a random float between 0 and 1:
float random = ((float) rand()) / (float) RAND_MAX;
- But what, if I want a function that given a range of two floats, returns a pseudorandom float in that range?
Example:
RandomFloat( 0.78, 4.5 ); //Could return 2.4124, 0.99, 4.1, etc.
Random between 2 float :
Random between 2 int :
This works by returning
a
plus something, where something is between 0 andb-a
which makes the end result lie in betweena
andb
.Suppose, you have MIN_RAND and MAX_RAND defining the ranges, then you can have the following:
This will provide you the number scaled to your preferred range. MIN_RAND, MAX_RAND can be any value, like say 2.5, 6.6 So, the function could be as: