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. QEMU命令配置虚拟机网络的用户模式

    QEMU缺省使用“-net nic-net user”参数为客户机配置网络,提供了一种用户模式( user-mode)的网络模拟.使用用户模式的客户机可以连通宿主机及外部网络.用户模式网络完全由QEM ...

  2. java和c# md5加密

    MD5加密的方式有很多,加盐的方式更多,最近项目需要java和c#加密结果一致,形成方法如下: 1.c#加密方法/// <summary> /// MD5 加密字符串 /// </s ...

  3. Linux 操作系统 & High Tech

    分享10大白帽黑客专用的 Linux 操作系统 - 51CTO.COMhttp://os.51cto.com/art/201905/597156.htm Ubuntu 创始人谈论为什么 Linux 在 ...

  4. 【Linux】Gitlab库已损坏前端显示500错误解决方法

    背景: 在进行gitlab数据迁移之后,所有页面正常访问,唯独在访问项目repo地址时,报500错误 1 查看日志: 命令查看: gitlab-ctl tail 或者手动查看:/var/log/git ...

  5. Elasticsearch技术解析与实战 PDF (内含目录)

    Elasticsearch技术解析与实战                                  介绍: Elasticsearch是一个强[0大0]的搜索引擎,提供了近实时的索引.搜索.分 ...

  6. ABAP DEMO ALV-监听数据修改

    *&---------------------------------------------------------------------* *& Report YDEMO_006 ...

  7. Unity3d基于Socket通讯例子(转)

    按语:按照下文,服务端利用网络测试工具,把下面客户端代码放到U3D中摄像机上,运行结果正确. http://www.manew.com/thread-102109-1-1.html 在一个网站上看到有 ...

  8. Python - Django - 页面上展示固定的页码数

    如果页数太多的话,全部显示在页面上就会显得很冗杂 可以在页面中显示规定的页码数 例如: book_list.html: <!DOCTYPE html> <html lang=&quo ...

  9. Delphi中进行延时的4种方法

     1.挂起,不占CPUsleep2.不挂起,占cpuprocedure Delay(msecs:integer);varFirstTickCount:longint;beginFirstTickCou ...

  10. 关于PF_INET和AF_INET的区别

    在写网络程序的时候,建立TCP socket:   sock = socket(PF_INET, SOCK_STREAM, 0);然后在绑定本地地址或连接远程地址时需要初始化sockaddr_in结构 ...