Math.random().toFixed(20)
allows you to generate a random number from 0 to 1 with 20 digits after the decimal point. And how can I generate random numbers with a large number of digits? After all, toFixed
a maximum of 20 digits. For example, I need a random number generator with 30 digits after the decimal point.
- Specify, why do you need a random number generator with 30 digits after the decimal point? - Kromster
|
2 answers
The type float stores 20 decimal places. If you need 30 characters, you can get in the form of a string like this:
Math.random().toFixed(20) + Math.random().toFixed(10).substring(2) // "0.121046188135246923782935929242"
But when converting to float, there will still be 20 characters:
parseFloat(Math.random().toFixed(20) + Math.random().toFixed(10).substring(2)) // "0.12104618813524692378"
|
Add the parts after the comma as a string, and put 0 in the beginning.
|