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;
Example:
RandomFloat( 0.78, 4.5 ); //Could return 2.4124, 0.99, 4.1, etc.
Syntax. The random. uniform() function returns a random floating-point number N such that start <= N <= stop . In simple words, uniform(10.5, 15.5) will generate any float number greater than or equal to 10.5 and less than or equal to 20.5.
In Python, the random. uniform() function gives a random floating-point number, and that is within a specified range. For example, it can produce a random float number in the range of 10 to 100. From 50.50 to 75.5, as an alternative.
To get a random floating number we will use random. uniform() function this will give a float number between a given range. We can also round these floating numbers using the round() function in Python.
float RandomFloat(float a, float b) { float random = ((float) rand()) / (float) RAND_MAX; float diff = b - a; float r = random * diff; return a + r; }
This works by returning a
plus something, where something is between 0 and b-a
which makes the end result lie in between a
and b
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With