Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用crypto.generateValues()生成0到26的随机数_Javascript_Typescript - Fatal编程技术网

Javascript 如何使用crypto.generateValues()生成0到26的随机数

Javascript 如何使用crypto.generateValues()生成0到26的随机数,javascript,typescript,Javascript,Typescript,如何使用crypto.getRandomValues()生成0到26的随机数。 帮助 正如建议的那样,您个人可以使用Math.random(),它是为这些事情而设计的: let number = Math.random() * 26 但在数学方面,您可以使用模块类N+1得到一个介于0和N之间的数字,如下所示: let number = yourFunctionThatUsesCrypto() % 27 // [0,26] let number = yourFunctionThatUsesCry

如何使用crypto.getRandomValues()生成0到26的随机数。 帮助


正如建议的那样,您个人可以使用
Math.random()
,它是为这些事情而设计的:

let number = Math.random() * 26
但在数学方面,您可以使用模块类N+1得到一个介于0和N之间的数字,如下所示:

let number = yourFunctionThatUsesCrypto() % 27 // [0,26]
let number = yourFunctionThatUsesCrypto() % 26 // [0,26)

否则,按照@Mr.Polywhill的建议,您可以将其乘以您想要的限制(在本例中为26)

如果您想要一个介于0和26之间的数字,您可能需要使用
Math.random()
…该对象使用的是crypto.getRandomValues()“该对象”是什么?。。
let number = yourFunctionThatUsesCrypto() % 27 // [0,26]
let number = yourFunctionThatUsesCrypto() % 26 // [0,26)