{"id":10241,"date":"2021-12-20T23:10:14","date_gmt":"2021-12-20T15:10:14","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10241"},"modified":"2021-12-28T17:19:27","modified_gmt":"2021-12-28T09:19:27","slug":"js-getrandomvalue-math-random","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/12\/js-getrandomvalue-math-random\/","title":{"rendered":"\u6df1\u5165JS getRandomValues\u548cMath.random\u65b9\u6cd5"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10241\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10241<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202112-random.jpg';if(!\/zhangxinxu\/.test(location.host)){document.body.addEventListener('mousedown',function(){location.href='https:\/\/www.zhangxinxu.com\/wordpress\/?p=10241'});}<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/rand-safe.jpg\" width=\"480\" height=\"304\" alt=\"\u968f\u673a\u4e0e\u5b89\u5168JS\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u5f15\u8a00<\/h3>\n<p>\u6211\u4eec\u65e5\u5e38\u5f00\u53d1\u7ecf\u5e38\u4f1a\u7528\u5230\u968f\u673a\u6570\uff0c\u57fa\u672c\u4e0a\u6211\u63a5\u89e6\u4e0b\u6765\uff0c\u90fd\u662f\u4f7f\u7528 <code>Math.random()<\/code> \u751f\u6210\u7684\u3002<\/p>\n<p>\u4f8b\u5982\u751f\u6210\u968f\u673aID\uff1a<\/p>\n<pre>document.body.id = ('_' + Math.random()).replace('0.', '');<\/pre>\n<p>\u8bf7\u95ee\u8fd9\u6837\u5b9e\u73b0\u6709\u6ca1\u6709\u95ee\u9898\uff1f<\/p>\n<p>\u56de\u7b54\uff1a\u6ca1\u6709\u95ee\u9898\u3002<\/p>\n<p>\u4f8b\u5982\u968f\u673a\u6392\u5e8f\uff1a<\/p>\n<pre>[1, 2, 3, 4, 5].sort(_ =&gt; Math.random() - .5);<\/pre>\n<p>\u8bf7\u95ee\u8fd9\u6837\u5b9e\u73b0\u6709\u6ca1\u6709\u95ee\u9898\uff1f<\/p>\n<p>\u56de\u7b54\uff1a\u6ca1\u6709\u95ee\u9898\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5b9e\u73b0\u52a0\u5bc6\u64cd\u4f5c\uff0c\u4f8b\u5982\u751f\u6210\u5bc6\u94a5\uff0c\u5c24\u5176\u662f\u5728 Node.js \u670d\u52a1\u5c42\uff0c\u5219 <code>Math.random()<\/code> \u5c31\u6709\u95ee\u9898\u4e86\uff0c\u4f1a\u6709\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\uff0c\u9700\u8981\u4f7f\u7528 <code>crypto.getRandomValues()<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u54e6\uff1f\u5b89\u5168\u98ce\u9669\uff1f\u8fd8\u6709\u4e2a <code>getRandomValues()<\/code> \u65b9\u6cd5\uff1f<\/p>\n<p>\u4e0d\u6025\uff0c\u6211\u4eec\u6162\u6162\u804a\u3002<\/p>\n<h3>\u4e8c\u3001Math.random\u7684\u5b89\u5168\u98ce\u9669<\/h3>\n<p>\u63d0\u5230 <code>Math.random()<\/code> \u7684\u5b89\u5168\u98ce\u9669\uff0c\u6709\u5f00\u53d1\u4eba\u5458\u4f1a\u8bf4\u56e0\u4e3a <code>Math.random()<\/code> \u8fd4\u56de\u7684\u662f\u4f2a\u968f\u673a\u6570\u3002<\/p>\n<p>\u8fd9\u4e2a\u89e3\u91ca\u4f3c\u662f\u800c\u975e\uff0c\u548c\u4f2a\u968f\u673a\u6570\u6ca1\u6709\u5173\u7cfb\uff0c<code>getRandomValues()<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u4e5f\u662f\u4f2a\u968f\u673a\u6570\u3002<\/p>\n<p>\u8fd8\u6709\u4eba\u8bf4\u56e0\u4e3a <code>Math.random()<\/code> \u8fd4\u56de\u7684\u968f\u673a\u503c\u8303\u56f4\u4e0d\u662f\u5747\u5300\u7684\uff0c\u8fd9\u4e2a\u56de\u7b54\u5c31\u4e0d\u662f\u4f3c\u662f\u800c\u975e\u4e86\uff0c\u800c\u662f\u5927\u9519\u7279\u9519\u3002<\/p>\n<p>\u4f8b\u5982\u6211\u8fd0\u884c\u4e2a2\u4e07\u6b21 <code>Math.random()<\/code> \u65b9\u6cd5\u7684\u5206\u5e03\u56fe\u662f\u4e0b\u9762\u8fd9\u6837\u7684\uff08\u5b9e\u65f6Canvas\u7ed8\u5236\uff0c\u5916\u7ad9\u65e0\u6548\u679c\uff0c\u70b9\u51fb\u91cd\u65b0\u7ed8\u5236\uff09\uff1a<\/p>\n<div style=\"display: inline-block;\">\n<div><canvas id=\"randCanvas\" width=\"400\" height=\"200\" style=\"max-width: 100%; width:320px; height: 160px; outline: 1px solid #eee;\"><\/canvas><\/div>\n<div style=\"display: flex;justify-content: space-between;\"><span>0<\/span><span>0.5<\/span><span>1<\/span><\/div>\n<\/div>\n<p><script>\n(function () {\n    var canvas = document.querySelector('#randCanvas');\n    var context = canvas.getContext('2d');\n    var width = canvas.width;\n    var height = canvas.height;\n    var arrX = Array(width).fill(height);\n    context.fillStyle = '#24828b'\n    var start = 0;\n    var maxLoop = 2000;\n    var isRun = true;\n    var draw = function () {\n        var x = Math.round(Math.random() * 400);\n        arrX[x] -= 2;\n        context.fillRect(x-1, arrX[x], 2, 2);\n    };\n    var req = function () {\n        requestAnimationFrame(function () {\n            Array(10).fill().forEach(draw);           \n            isRun = true;\n            if (++start <= maxLoop) {\n                req();\n            } else {\n                isRun = false;\n            }\n        });\n    };\n    req();\n\n    canvas.addEventListener('click', function () {\n        context.clearRect(0, 0, width, height);\n        start = 0;\n        arrX = Array(width).fill(height);\n        if (!isRun) {\n            setTimeout(req, 20);\n        }\n    });\n})();\n<\/script><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u4ece\u5de6\u5230\u53f3\u57fa\u672c\u4e0a\u662f\u5747\u5300\u7684\u3002<\/p>\n<p>\u90a3\u7a76\u7adf\u4e3a\u4f55\u662f\u4e0d\u5b89\u5168\u7684\u5462\uff1f<\/p>\n<p>\u8fd9\u4e2a\u5c31\u8981\u8bb2\u8bb2 <code>Math.random()<\/code> \u65b9\u6cd5\u7684\u5e95\u5c42\u5b9e\u73b0\u4e86\uff0c\u8fd9\u91cc\u6709<a href=\"https:\/\/www.anquanke.com\/post\/id\/231799\" target=\"_blank\" rel=\"nofollow noopener\">\u4e00\u7bc7\u6587\u7ae0<\/a>\u6709\u6df1\u5165\u4ecb\u7ecd\uff0c\u6211\u7b80\u8ff0\u4e0b\u5176\u4e2d\u7684\u8981\u70b9\u3002<\/p>\n<p><code>Math.random()<\/code> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8303\u56f40-1\u7684\u4f2a\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u5176\u5728 V8 \u4e2d\u7684\u5b9e\u73b0\u539f\u7406\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<div style=\"background-color: #f5f5f5;border-left:solid #ddd;padding:6px 8px;\">\n<p>\u4e3a\u4e86\u4fdd\u8bc1\u8db3\u591f\u7684\u6027\u80fd\uff0c<code>Math.random()<\/code> \u968f\u673a\u6570\u5e76\u4e0d\u662f\u5b9e\u65f6\u751f\u6210\u7684\uff0c\u800c\u662f\u76f4\u63a5\u751f\u6210\u4e00\u7ec4\u968f\u673a\u6570\uff0864\u4e2a\uff09\uff0c\u5e76\u653e\u5728\u7f13\u5b58\u4e2d\u3002<\/p>\n<p>\u5f53\u8fd9\u4e00\u7ec4\u968f\u673a\u6570\u53d6\u5b8c\u4e4b\u540e\u518d\u91cd\u65b0\u751f\u6210\u4e00\u6279\uff0c\u653e\u5728\u7f13\u5b58\u4e2d\u3002<\/p>\n<\/div>\n<p>\u7531\u4e8e <code>Math.random()<\/code> \u7684\u5e95\u5c42\u7b97\u6cd5\u662f\u516c\u5f00\u7684\uff08xorshift128+ \u7b97\u6cd5\uff09\uff0cV8 \u6e90\u7801\u53ef\u89c1\uff0c\u56e0\u6b64\uff0c\u662f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u8bed\u8a00\u6a21\u62df\u7684\uff0c\u8fd9\u5c31\u5bfc\u81f4\uff0c\u5982\u679c\u653b\u51fb\u8005\u77e5\u9053\u4e86\u5f53\u524d\u968f\u673a\u751f\u6210\u5668\u7684\u72b6\u6001\uff0c\u90a3\u5c31\u53ef\u4ee5\u77e5\u9053\u7f13\u5b58\u4e2d\u7684\u6240\u6709\u968f\u673a\u6570\uff0c\u90a3\u5c31\u5f88\u5bb9\u6613\u5339\u914d\u4e0e\u7834\u89e3\u3002<\/p>\n<p>\u4f8b\u5982\u62bd\u5956\u6d3b\u52a8\uff0c\u4f7f\u7528 <code>Math.random()<\/code> \u8fdb\u884c\u968f\u673a\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u4f30\u7b97\u51fa\u4e00\u6bb5\u65f6\u95f4\u5185\u6240\u6709\u7684\u4e2d\u5956\u7ed3\u679c\uff0c\u4ece\u800c\u5e26\u6765\u975e\u5e38\u4e25\u91cd\u4e14\u81f4\u547d\u7684\u635f\u5931\u3002<\/p>\n<p>\u6b64\u65f6\u5e94\u8be5\u4f7f\u7528 <code>getRandomValues()<\/code> \u65b9\u6cd5\u3002<\/p>\n<p><small class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4e0d\u662f\u539f\u6587\u7ad9\u70b9\uff0c\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u5728\u8fd9\u91cc\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=10241\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/small><\/p>\n<h3>\u4e09\u3001\u4e86\u89e3getRandomValues\u65b9\u6cd5<\/h3>\n<p><code>Crypto.getRandomValues()<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u4e5f\u662f\u4f2a\u968f\u673a\u6570\uff0c\u4e0d\u662f\u771f\u968f\u673a\uff0c\u6309\u7167 MDN \u7684\u8bf4\u6cd5\uff0c\u662f\u4e3a\u4e86\u6027\u80fd\u8003\u8651\uff0c\u6ca1\u6709\u4f7f\u7528\u771f\u968f\u673a\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u6309\u7167\u6211\u7684\u8ba4\u8bc6\uff0c\u6240\u6709\u53ef\u4ee5\u4f7f\u7528\u7b97\u6cd5\u751f\u6210\u7684\u968f\u673a\u6570\u90fd\u53ef\u4ee5\u770b\u6210\u662f\u4f2a\u968f\u673a\u6570\uff0c\u771f\u968f\u673a\u6570\u5e94\u8be5\u662f\u5b58\u5728\u81ea\u7136\u754c\uff0c\u4f8b\u5982\u7c92\u5b50\u7684\u8d77\u4f0f\uff0c\u58f0\u97f3\u7684\u566a\u70b9\uff0c\u5206\u5b50\u7684\u5206\u5e03\u7b49\u3002<\/p>\n<blockquote><p>\u7ea6\u7ff0\u00b7\u51af\u00b7\u8bfa\u4f0a\u66fc\uff08\u73a9\u7b11\u8bdd\uff09\uff1a\u4efb\u4f55\u4f7f\u7528\u7b97\u672f\u65b9\u6cd5\u751f\u6210\u968f\u673a\u6570\u7684\u4eba\uff0c\u90fd\u662f\u6709\u7f6a\u7684<\/p><\/blockquote>\n<p>\u548c <code>Math.random()<\/code> \u65b9\u6cd5\u7684\u533a\u522b\u5728\u4e8e\uff0c<code>getRandomValues()<\/code> \u65b9\u6cd5\u7684\u968f\u673a\u79cd\u5b50\u751f\u6210\u5668\u66f4\u52a0\u7684\u65e0\u5e8f\uff0c\u4f8b\u5982\u7cfb\u7edf\u5c42\u9762\u7684\u65e0\u5e8f\u6e90\uff08\u6709\u4e9b\u786c\u4ef6\u81ea\u5e26\u968f\u673a\u79cd\u5b50\uff09\u3002<\/p>\n<p>\u7136\u540e\u4e0d\u540c\u6d4f\u89c8\u5668\u4e0b <code>getRandomValues()<\/code> \u65b9\u6cd5\u751f\u6210\u7684\u968f\u673a\u6570\u53ef\u80fd\u662f\u6709\u533a\u522b\u7684\u3002<\/p>\n<p>\u4ee5\u53ca <code>getRandomValues()<\/code> \u65b9\u6cd5\u7684\u5e95\u5c42\u5b9e\u73b0\u662f\u6ca1\u6709\u7f13\u5b58\u7684\uff0c\u968f\u673a\u6570\u90fd\u662f\u5b9e\u65f6\u751f\u6210\u7684\uff0c\u56e0\u6b64\uff0c\u6027\u80fd\u4e0a\u662f\u8981\u6bd4 <code>Math.random()<\/code> \u5dee\u7684\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u662f\u9ad8\u5e76\u53d1\u7684\u573a\u666f\uff0c\u540c\u65f6\u968f\u673a\u6570\u4ec5\u4ec5\u662f\u7528\u505a\u968f\u673a\uff0c\u4e0e\u5b89\u5168\u548c\u91d1\u94b1\u4e0d\u76f8\u5173\uff0c\u8bf7\u4f7f\u7528 <code>Math.random()<\/code> \u800c\u4e0d\u662f <code>getRandomValues()<\/code>\u3002<\/p>\n<p>\u5c31 Web \u524d\u7aef\u800c\u8a00\uff0c\u5fc5\u987b\u8981\u4f7f\u7528 <code>getRandomValues()<\/code> \u65b9\u6cd5\u7684\u573a\u666f\u5f88\u5c11\uff0c\u4e0d\u8fc7\u7531\u4e8e\u7eaf\u524d\u7aef\u51e0\u4e4e\u4e0d\u5b58\u5728\u6240\u8c13\u7684\u9ad8\u5e76\u53d1\uff0c\u56e0\u6b64\uff0c\u4f60\u4f7f\u7528 <code>getRandomValues()<\/code> \u65b9\u6cd5\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u6709\u88c5\u903c\u7684\u4f5c\u7528\u3002<\/p>\n<h4>\u8bed\u6cd5\u548c\u4f7f\u7528<\/h4>\n<p>\u4f7f\u7528\u793a\u610f\uff08\u4e0b\u9762\u4ee3\u7801 self \u4e0d\u592a\u4e86\u89e3\u53ef\u4ee5\u53c2\u89c1\u6b64\u6587\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2017\/07\/js-window-self\/\">\u4e86\u89e3\u5168\u5c40\u4f5c\u7528\u57dfself<\/a>\u201d\uff09\uff1a<\/p>\n<pre>let randNumber = self.crypto.getRandomValues(new Uint32Array(1))[0];\r\n\/\/ \u4e00\u4e32\u968f\u673a\u6574\u6570\uff0c\u901a\u5e3810\u4f4d\r\nconsole.log(randNumber);<\/pre>\n<p>\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>crypto.getRandomValues(typedArray)<\/pre>\n<p>\u652f\u6301\u7684\u53c2\u6570 <code>typedArray<\/code> \u8868\u793a\u6574\u6570\u578b\u7684\u7c7b\u578b\u6570\u7ec4\uff0c\u5305\u62ec\uff1aInt8Array, Uint8Array, Int16Array, Uint16Array, Int32Array \u6216\u8005 Uint32Array\u3002<\/p>\n<p>\u8fd4\u56de\u503c\u56de\u662f\u6240\u6709\u88ab\u66ff\u6362\u4e3a\u968f\u673a\u6570\u7684\u65b0\u7684\u6570\u7ec4\u3002<\/p>\n<p>\u4e0d\u8fc7 <code>getRandomValues()<\/code> \u65b9\u6cd5\u540d\u79f0\u6709\u4e9b\u957f\uff0c\u4e0d\u5229\u4e8e\u8bb0\u5fc6\u548c\u654f\u6377\u4f7f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u9020\u4e0b\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>Math.randomValue = function () {\r\n    return self.crypto.getRandomValues(new Uint32Array(1))[0];\r\n};<\/pre>\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 <code>Math.randomValue()<\/code> \u65b9\u6cd5\u8fd4\u56de\u8db3\u591f\u5b89\u5168\u7684\u968f\u673a\u503c\u4e86\u3002<\/p>\n<h4>IE\u6d4f\u89c8\u5668<\/h4>\n<p>\u5728 IE11 \u6d4f\u89c8\u5668\u4e0b\u9700\u8981\u6dfb\u52a0 ms \u79c1\u6709\u524d\u7f00\uff1a<\/p>\n<pre>window.msCrypto.getRandomValues()<\/pre>\n<p>IE10 \u4e0d\u652f\u6301 <code>getRandomValues()<\/code> \u65b9\u6cd5\u3002<\/p>\n<h3>\u56db\u3001Crypto\u5bf9\u8c61\u4e0e\u5176\u4ed6\u968f\u673a\u503c<\/h3>\n<h4>UUID\u751f\u6210<\/h4>\n<p>\u9664\u4e86\u751f\u6210\u968f\u673a\u6570\uff0cCrypto\u5bf9\u8c61\u8fd8\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5b57\u7b26\u957f\u5ea6\u4e3a36\u7684 UUID \uff08Universally Unique Identifier\u7684\u7f29\u5199\uff0c\u8868\u793a\u552f\u4e00\u901a\u7528\u6807\u8bc6\u7b26\uff09\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>let uuid = self.crypto.randomUUID();\r\nconsole.log(uuid);\r\n\/\/ \u793a\u610f\u8f93\u51fa\uff1a2433df46-d77f-4eb9-bbdd-4cd99361fe08<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/uuid-min.png\" width=\"332\" height=\"125\" alt=\"uuid\u8fd0\u884c\u7ed3\u679c\u622a\u56fe\" class=\"alignnone size-medium\" style=\"border:1px solid #ddd;\" \/><\/p>\n<p>\u4e0d\u8fc7\u8fd9\u4e2a API \u662f\u4eca\u5e74\u624d\u652f\u6301\u7684\uff08Chrome 92+\uff09\uff0c\u8fd8\u6bd4\u8f83\u65b0\uff0c\u5927\u5bb6\u5148\u4e86\u89e3\u4e0b\uff0c\u7b49\u660e\u5e74\u8fd9\u4e2a\u65f6\u5019\uff0c\u5dee\u4e0d\u591a\u53ef\u4ee5\u5728\u4e00\u4e9b\u524d\u6cbf\u9879\u76ee\u4e2d\u5c1d\u8bd5\u4f7f\u7528\u4e86\u3002<\/p>\n<h4>crypto.subtle\u5bf9\u8c61<\/h4>\n<p><code>crypto<\/code> \u5bf9\u8c61\u8fd8\u652f\u6301\u4e00\u4e2a\u540d\u4e3a <code>subtle<\/code> \u7684\u5c5e\u6027\uff0c\u76ee\u524d\u552f\u4e00\u7684\u5c5e\u6027\uff0c\u8fd4\u56de\u7684\u5c5e\u6027\u503c\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u79f0\u4e3a SubtleCrypto \u5bf9\u8c61\uff0c\u5305\u542b\u5927\u91cf\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5404\u79cd\u7b7e\u540d\u548c\u5bc6\u94a5\uff0c\u6240\u6709\u65b9\u6cd5\u5747\u8fd4\u56de Promise\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>SubtleCrypto.encrypt()<\/li>\n<li>SubtleCrypto.decrypt()<\/li>\n<li>SubtleCrypto.sign()<\/li>\n<li>SubtleCrypto.verify()<\/li>\n<li>SubtleCrypto.digest()<\/li>\n<li>SubtleCrypto.generateKey()<\/li>\n<li>SubtleCrypto.deriveKey()<\/li>\n<li>SubtleCrypto.deriveBits()<\/li>\n<li>SubtleCrypto.importKey()<\/li>\n<li>SubtleCrypto.exportKey()<\/li>\n<li>SubtleCrypto.wrapKey()<\/li>\n<li>SubtleCrypto.unwrapKey()<\/li>\n<\/ul>\n<p>\u7531\u4e8e\u8fd9\u4e9b\u65b9\u6cd5\u975e\u672c\u6587\u91cd\u70b9\uff0c\u4ee5\u53ca\u6211\u4eec\u65e5\u5e38\u5f00\u53d1\u5f88\u5c11\u4f1a\u7528\u5230\uff0c\u6240\u4ee5\u4e0d\u5c55\u5f00\u4ecb\u7ecd\uff0c\u6709\u5174\u8da3\u53ef\u4ee5\u53bb <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/SubtleCrypto\">MDN \u6587\u6863<\/a>\u67e5\u770b\u3002<\/p>\n<h3>\u4e94\u3001\u7ed3\u8bed<\/h3>\n<p>\u6211\u4eec\u65e5\u5e38\u5f00\u53d1\u5f00\u59cb\u63a8\u8350\u4f7f\u7528 <code>Math.random()<\/code> \u65b9\u6cd5\uff0c\u9ad8\u6027\u80fd\u4e14\u5b9e\u7528\uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u7684\u968f\u673a\u503c\u4e0e\u52a0\u5bc6\u76f8\u5173\uff0c\u6216\u8005\u6d89\u53ca\u5230\u91d1\u94b1\u7b49\u5b89\u5168\u6027\u8981\u6c42\u975e\u5e38\u9ad8\u7684\u573a\u666f\uff0c\u52a1\u5fc5\u4f7f\u7528 <code>getRandomValues()<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u597d\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u3002<\/p>\n<p>\u662f\u4e0d\u662f\u5b66\u5230\u4e86\u4e0d\u5c11\u77e5\u8bc6\u5462\uff0c\u90a3\u8fd8\u7b49\u4ec0\u4e48\uff0c\u8d76\u5feb<button onclick=\"shareWeixin.click()\" class=\"entry-share-btn\">\u5206\u4eab<\/button>\u7ed9\u4f60\u7684\u5c0f\u4f19\u4f34\u5427\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f913.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10241\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10241<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202112\/rand-safe.jpg\" width=\"160\" height=\"101\" alt=\"\u968f\u673a\u4e0e\u5b89\u5168JS\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u672c\u6587\u5e26\u5927\u5bb6\u6df1\u5165\u4e86\u89e3\u4e0b\u4e3a\u4ec0\u4e48\u6d89\u53ca\u5b89\u5168\u4e0e\u8d22\u52a1\u7684\u65f6\u5019\uff0c\u4e0d\u80fd\u4f7f\u7528 Math.random \u65b9\u6cd5\u8fdb\u884c\u968f\u673a\uff0c\u5e72\u8d27\u8fd8\u4e0d\u5c11\uff0c\u8d70\u8fc7\u8def\u8fc7\u4e0d\u8981\u9519\u8fc7\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1808,1810,106,1805,728,1807,1145,1496,1105],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10241"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=10241"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10241\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=10241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=10241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=10241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}