范围JavaScript中的随机大整数

时间:2021-09-15 16:53:20

I need a random integer between 0 and an integer with over 1000 decimal places.

我需要一个介于0和整数之间的随机整数,小数位数超过1000。

Working with integers this large is easy with: big-integer (NPM), but there is no random method, and Math.random() doesn't express enough precision to cover the domain.

使用整数这个大的很容易:大整数(NPM),但是没有随机方法,并且Math.random()没有足够的精度来覆盖域。

Does anyone know a clever way of generating pseudo random integers in a large range in JavaScript?

有没有人知道在JavaScript中大范围生成伪随机整数的聪明方法?

2 个解决方案

#1


2  

I think best solution for your case in Node.JS is crypto.randomBytes(size, [callback]);

我认为在Node.JS中你的案例的最佳解决方案是crypto.randomBytes(size,[callback]);

#2


0  

Perhaps this:

也许这个:

var crypto = require('crypto');
var biformat = require('biguint-format');

// Adjust # bytes as needed
var seed = biformat(crypto.randomBytes(8), 'dec');
bigInt(seed);

#1


2  

I think best solution for your case in Node.JS is crypto.randomBytes(size, [callback]);

我认为在Node.JS中你的案例的最佳解决方案是crypto.randomBytes(size,[callback]);

#2


0  

Perhaps this:

也许这个:

var crypto = require('crypto');
var biformat = require('biguint-format');

// Adjust # bytes as needed
var seed = biformat(crypto.randomBytes(8), 'dec');
bigInt(seed);