I'm trying to generate a random number that must have a fixed length of exactly 70 digits.
I don't know if javascript
Math.floor((Math.random()*1000000)+1);
would ever create a number less than 70 digits?
I'm trying to generate a random number that must have a fixed length of exactly 70 digits.
I don't know if javascript
Math.floor((Math.random()*1000000)+1);
would ever create a number less than 70 digits?
let randomString = '';
for(var i=0; i<70; i++){
randomString+=Math.floor(Math.random()*9);
}
console.log(randomString);
The maximum you can encode in a 64 bit number is
9,223,372,036,854,775,807
(2^63 − 1
), a 19 digits number.
If you want to have a 70 digit number, you won't be able to represent it using regular numbers. You will have to use a string representation or create your own representation.
If you want to generate a random string of 70 digits, you can do it in a single line:
const random = new Array(70).fill('').map(() => Math.floor(Math.random() * 9)).join('');
console.log(random);
You can even shorten the array creation (see @ygorbunkov comment below):
const random = [...Array(70)].map(() => Math.floor(Math.random() * 9)).join('');
console.log(random);
This is working perfectly fine. It return string of 70 numbers
let number = (Math.random().toFixed(70) * Math.pow(10,70)).toPrecision(70).substring(0,70);
console.log(number);