Math.random()

返回介于 0(包含) ~ 1(不包含) 之间的一个随机数。
Math.random()函数不是加密安全的随机数生成器。

window.crypto.getRandomValues

Crypto.getRandomValues() 方法让你可以获取符合密码学要求的安全的随机值。传入参数的数组被随机值填充(在加密意义上的随机)。

为了确保足够的性能,不使用真正的随机数生成器,但是它们正在使用具有足够熵值伪随机数生成器。它所使用的 PRNG 的实现与其他不同,但适用于加密的用途。该实现还需要使用具有足够熵的种子,如系统级熵源。

语法

cryptoObj.getRandomValues(typedArray);

参数

  typedArray是一个基于整数的 TypedArray,它可以是 Int8ArrayUint8ArrayInt16Array、 Uint16Array、 Int32Array 或者 Uint32Array。在数组中的所有的元素会被随机数重写。(注释:生成的随机数储存在 typedArray 数组上。)

例子

window.crypto.getRandomValues(new Int8Array(2))//Int8Array(2) [44, -3]
window.crypto.getRandomValues(new Uint8Array(2))//Uint8Array(2) [218, 119]
window.crypto.getRandomValues(new Int16Array(2))//Int16Array(2) [24582, -15808]
window.crypto.getRandomValues(new Uint16Array(2))//Uint16Array(2) [55391, 55756]
window.crypto.getRandomValues(new Int32Array(2))//Int32Array(2) [1574608122, -836595554]
window.crypto.getRandomValues(new Uint32Array(2))//Uint32Array(2) [1906545366, 2391348462]

Math.random()的等价表达式:window.crypto.getRandomValues(new Uint8Array(1)) * 0.001

详见:https://developer.mozilla.org/zh-CN/docs/Web/API/RandomSource/getRandomValues

Math.random()的加密安全替换方法window.crypto.getRandomValues的更多相关文章

  1. 通过window.crypto.getRandomValues获得一个大于零的随机数

    window.crypto.getRandomValues(new Uint32Array(1))[0]; 浏览器支持情况如下: IE: no IE Mobile: no Firefox24+ Fir ...

  2. java中random的几个方法的使用Math.random()和random().

    random java中我们有时候也需要使用使用random来产生随机数,下面我来简单的介绍下java中random的使用方法 第一种:Math.random() public static doub ...

  3. Random.nextInt()替换Math.random()

    在项目中使用哪个随机数 文章参考 http://liukai.iteye.com/blog/433718 今天用了find bugs后查出来了个问题 Google了下 发现 Random.nextin ...

  4. Math.random 随机数方法

    随机取数方法 Math.random() 表示0到1之间随机取一个数 <x< 小数 Math.random()* 表示0<x< parseInt(Math.random()*) ...

  5. Random类和Math.random()方法

    一.Random类的定义Random类位于 java.util 包中,主要用于生成伪 随机数Random类将 种子数 作为随机算法的起源数字,计算生成伪随机数,其与生成的随机数字的区间无关创建Rand ...

  6. 生成随机数的几种方法、Math.random()随机数的生成、Random()的使用

    第一种方法使用:System.currentTimeMillis(); final long l = System.currentTimeMillis(); final int rs = (int) ...

  7. javascript 中根据sort 方法随机数组 (Math.random)

    var arr = [1,2,3,4,5,6,7,8,9,10]; function Arandom(a,b){ return (Math.random() > 0.5) ? 1 : -1;; ...

  8. Java利用Math.random()方法随机生成A-Z的字符

    package reverse; import java.text.DecimalFormat; public class Reverse { public static void main(Stri ...

  9. Math.random引发的骗术,绝对是用随机数骗前端妹纸的最佳方法

    我觉得今天我运气特好,今天我们来赌一赌,我们来搞个随机数,Math.floor(Math.random() * 10),如果这个数等于0到7,这个月的饭,我全请了,如果是8或9,你就请一个礼拜成不?于 ...

随机推荐

  1. face-morpher过程函数分析

    01 dlib.get_frontal_face_detector #功能:人脸检测画框#参数:无#返回值:默认的人脸检测器 02 points1.astype 转换数组的数据类型 03 np.mea ...

  2. open jdk性能与稳定性测试比较(转载)

    因为oracle jdk从jdk8u201之后就不提供免费下载了,所以最近在看openjdk的分支实现,网上搜了下,有下列选择和比较(我们目前主要在跑的是open jdk,不少人推荐的zulu ope ...

  3. 20161209pod search 'fmdb'提示[!] Unable to find a pod with name, author, summary, or description matching `fmdb`

    从SVN上更新工程之后运行工程提示错误: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update y ...

  4. (转)golang获取当前时间、时间戳和时间字符串及它们之间的相互转换

    原文连接: https://blog.csdn.net/skh2015java/article/details/70051512 1.获取当前时间 currentTime:=time.Now() // ...

  5. Mac 快速进入mysql命令行

    1.终端输入进入bin 目录 cd /usr/local/mysql/bin/ 2.mysql登录,输入密码即可 ./mysql -uroot -p 前提:mysql 服务已启动

  6. ES6深入浅出-13 Proxy 与 Reflect-1.Reflect 反射

    阮一峰  http://es6.ruanyifeng.com/#docs/reflect MDN有一些简陋的介绍 https://developer.mozilla.org/zh-CN/docs/We ...

  7. 软件定义网络基础---SDN的核心思想

    一:SDN包含的核心思想:解耦,抽象,可编程 二:解耦 (一)SDN网络解耦思想 解耦是指将控制平面和数据平面进行分离,主要为了解决传统网络中控制平面和数据平面在物理上紧耦合导致的问题 控制平面和数据 ...

  8. pandas绘制矩阵散点图(scatter_matrix)的方法

    以 sklearn的iris样本为数据集 import matplotlib.pyplot as plt from scipy import sparse import numpy as np imp ...

  9. v关于使用Glide加载图片失败时显示自己特定的图片

    Glide是Android加载图片的一个框架. 常用加载图片到imageView:Glide.with(this).load(url).into(ImageView imageview). 当加载失败 ...

  10. 让Chrome浏览器抓包接口数据秒变 python 代码

    简介 uncurl是一个库,允许您将curl请求转换为使用requests 的python代码.由于Chrome网络检查器具有的“copy as cURL”,因此该工具对于用python重新创建浏览器 ...