What is precision of MYSQL RAND() function? and how to generate huge random numbers?

Viewed 86

What is precision of MYSQL RAND() function?

I can't find it on the official page: MYSQL RAND() function is told to return floating-point number, unfortunately it's precision is not stated in a clear way. It can be a single-precision floating-point data, or double-precision, or any other kind of data.

What I would like to know exactly is - what is the maximum integer range [0,N] in which I can generate random integer numbers with FLOOR(RAND()*N) such that there won't be any "skips" and any number from 0 to N can be generated?

Another thing which I would like to know: How to generate numbers, which are bigger than N in MySQL?

1 Answers

As written in the MySQL docs the precision is system dependent. So there is not the one answer to your question.

https://dev.mysql.com/doc/internals/en/floating-point-types.html

Since MySQL uses the machine-dependent binary representation of float and double to store values in the database, we have to care about these. Today, most systems use the IEEE standard 754 for binary floating-point arithmetic. It describes a representation for single precision numbers as 1 bit for sign, 8 bits for biased exponent and 23 bits for fraction and for double precision numbers as 1-bit sign, 11-bit biased exponent and 52-bit fraction. However, we can not rely on the fact that every system uses this representation. Luckily, the ISO C standard requires the standard C library to have a header float.h that describes some details of the floating point representation on a machine. The comment above describes the value DBL_DIG. There is an equivalent value FLT_DIG for the C data type float.

At the end I have no clue why the precision of a random number is important in any case. I cannot see any use case

Related