Random Float Number Generation

Random float number generation

rand() can be used to generate pseudo-random numbers in C++. In combination with RAND_MAX and a little math, you can generate random numbers in any arbitrary interval you choose. This is sufficient for learning purposes and toy programs. If you need truly random numbers with normal distribution, you'll need to employ a more advanced method.


This will generate a number from 0.0 to 1.0, inclusive.

float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);

This will generate a number from 0.0 to some arbitrary float, X:

float r2 = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/X));

This will generate a number from some arbitrary LO to some arbitrary HI:

float r3 = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));

Note that the rand() function will often not be sufficient if you need truly random numbers.


Before calling rand(), you must first "seed" the random number generator by calling srand(). This should be done once during your program's run -- not once every time you call rand(). This is often done like this:

srand (static_cast <unsigned> (time(0)));

In order to call rand or srand you must #include <cstdlib>.

In order to call time, you must #include <ctime>.

How to get a random number between a float range?

Use random.uniform(a, b):

>>> import random
>>> random.uniform(1.5, 1.9)
1.8733202628557872

How to generate random float number in C

Try:

float x = (float)rand()/(float)(RAND_MAX/a);

To understand how this works consider the following.

N = a random value in [0..RAND_MAX] inclusively.

The above equation (removing the casts for clarity) becomes:

N/(RAND_MAX/a)

But division by a fraction is the equivalent to multiplying by said fraction's reciprocal, so this is equivalent to:

N * (a/RAND_MAX)

which can be rewritten as:

a * (N/RAND_MAX)

Considering N/RAND_MAX is always a floating point value between 0.0 and 1.0, this will generate a value between 0.0 and a.

Alternatively, you can use the following, which effectively does the breakdown I showed above. I actually prefer this simply because it is clearer what is actually going on (to me, anyway):

float x = ((float)rand()/(float)(RAND_MAX)) * a;

Note: the floating point representation of a must be exact or this will never hit your absolute edge case of a (it will get close). See this article for the gritty details about why.

Sample

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

int main(int argc, char *argv[])
{
srand((unsigned int)time(NULL));

float a = 5.0;
for (int i=0;i<20;i++)
printf("%f\n", ((float)rand()/(float)(RAND_MAX)) * a);
return 0;
}

Output

1.625741
3.832026
4.853078
0.687247
0.568085
2.810053
3.561830
3.674827
2.814782
3.047727
3.154944
0.141873
4.464814
0.124696
0.766487
2.349450
2.201889
2.148071
2.624953
2.578719

How to generate random float numbers?

You can use the format method in python if you want EXACTLY 2 digits after the decimal point (for example if you want 0.90):

import random

x = random.random()
print("{:.2f}".format(x))

or if you are fine with numbers like 0.9:

import random

x = random.random()
print(round(x,2))

how to generate a floating point random number with two precision in python

You can use round function with uniform function to limit float number to two decimal places.

Example:

 round(random.uniform(1.5, 1.9),2)
Out[]: 1.62

round(random.uniform(1.5, 1.9),3)
Out[]: 1.885

Setting a range for Random float generation in Ada

Looks like homework... I usually don't give the answer for such a task :)

First of all, you should get a warning when compiling your code as Min and Max are uninitialized when building your subtype.

To avoid this, the subtype must be declared after values are known.
To do this, declare your subtype inside a declare block.

About the random generation, getting a value inside your range is only a matter of translating and scaling.

In the end, I would do something like this.

with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;

procedure test1 is

Min, Max : Float;

RF : Generator;

begin

Reset(RF);

Get(Min);
Get(Max);
declare
subtype Custom is Float range Min .. Max;
Answer : Custom;
begin
Answer := (Random(RF) * (Max - Min)) + Min;
Put(Answer, Fore => 1, Aft => 2, Exp => 0);
end;
end test1;

In this particular case, the subtype is quite useless here.



Related Topics



Leave a reply



Submit