Javascript:使用crypto.getRandomValues在一个范围内生成一个随机数 [英] Javascript: Generate a random number within a range using crypto.getRandomValues
问题描述
我知道您可以使用此函数在一个范围内的JavaScript中生成一个随机数:
function getRandomInt(min,max ){
return Math.floor(Math.random()*(max - min + 1))+ min;
}
感谢IonuţG. Stan 此处。
我想知道的是,如果您可以生成更好地使用加密。 getRandomValues(),而不是Math.random()。我想生成一个介于0和10之间的数字,包括0和1,或者0 - 1,甚至10 - 5000。
你会注意到Math.random ()产生一个数字,例如: 0.8565239671015732 。
getRandomValues API可能会返回:
$ b b
- 231 与
Uint8Array(1)
-
355282741 与
Uint16Array(1) Uint32Array(1)
。
那么如何将它转换回十进制数字,上面同样的范围算法?或者我需要一个新的算法?
这是我尝试的代码,但它不能很好地工作。
function getRandomInt(min,max){
//创建字节数组并填充1个随机数
var byteArray = new Uint8Array(1);
window.crypto.getRandomValues(byteArray);
//转换为十进制
var randomNum ='0'+ byteArray [0] .toString();
//获取范围内的数字
randomNum = Math.floor(randomNum *(max - min + 1))+ min;
return randomNum;
}
在低端(范围0 - 1) 。使用getRandomValues()最好的方法是什么?
非常感谢
最简单的方法可能是拒绝抽样(请参阅 http://en.wikipedia.org/wiki/Rejection_sampling)。例如,假设 max - min
小于256:
函数getRandomInt(min,max){
//创建字节数组并填充1个随机数字
var byteArray = new Uint8Array(1);
window.crypto.getRandomValues(byteArray);
var range = max - min + 1;
var max_range = 256;
if(byteArray [0]> = Math.floor(max_range / range)* range)
return getRandomInt(min,max);
return min +(byteArray [0]%range);
}
I understand you can generate a random number in JavaScript within a range using this function:
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Courtesy of Ionuț G. Stan here.
What I want to know is if you can generate a better random number in a range using crypto.getRandomValues() instead of Math.random(). I would like to be able to generate a number between 0 and 10 inclusive, or 0 - 1, or even 10 - 5000 inclusive.
You'll note Math.random() produces a number like: 0.8565239671015732.
The getRandomValues API might return something like:
- 231 with
Uint8Array(1)
- 54328 with
Uint16Array(1)
- 355282741 with
Uint32Array(1)
.
So how to translate that back to a decimal number so I can keep with the same range algorithm above? Or do I need a new algorithm?
Here's the code I tried but it doesn't work too well.
function getRandomInt(min, max) {
// Create byte array and fill with 1 random number
var byteArray = new Uint8Array(1);
window.crypto.getRandomValues(byteArray);
// Convert to decimal
var randomNum = '0.' + byteArray[0].toString();
// Get number in range
randomNum = Math.floor(randomNum * (max - min + 1)) + min;
return randomNum;
}
At the low end (range 0 - 1) it returns more 0's than 1's. What's the best way to do it with getRandomValues()?
Many thanks
The easiest way is probably by rejection sampling (see http://en.wikipedia.org/wiki/Rejection_sampling). For example, assuming that max - min
is less than 256:
function getRandomInt(min, max) {
// Create byte array and fill with 1 random number
var byteArray = new Uint8Array(1);
window.crypto.getRandomValues(byteArray);
var range = max - min + 1;
var max_range = 256;
if (byteArray[0] >= Math.floor(max_range / range) * range)
return getRandomInt(min, max);
return min + (byteArray[0] % range);
}
这篇关于Javascript:使用crypto.getRandomValues在一个范围内生成一个随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!