Search code examples
randomprecisionmaxima

How to compute random bfloat number in Maxima CAS


Maxima Cas random function takes as an input floating point number and gives floating point number as an output.

I need floating point number with more digits, so I use bfloat with increased precision.

I have tried :

random(1.0b0)
bfloat(random(1.0));

The best result was :

bfloat(%pi)/6.000000000000000000000000000000000000000000b0 5.235987755982988730771072305465838140328615665625176368291574320513027343810348331046724708903528447b-1

but it is not random.


Solution

  • One way to generate a random bigfloat is to generate an integer with the appropriate number of bits and then rescale it to get a number in the range 0 to 1.

    Note that random(n) returns an integer in the range 0 to n - 1 when n is an integer, therefore: bfloat(random(10^fpprec) / 10^fpprec).