Java\u4e2d\u7684\u968f\u673a\u6570<\/strong><\/span><\/div>\n\u6211\u4eec\u9700\u8981\u5728Java\u4e2d\u968f\u673a\u751f\u6210\u4e00\u4e2a\u6570\u5b57\u3002java\u5f00\u53d1\u4e2d\u6211\u4eec\u901a\u5e38\u4f7f\u7528java.util.Random\u6765\u641e\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f2a\u968f\u673a\u7684\u751f\u6210\u673a\u5236\u3002Jvm \u901a\u8fc7\u4f20\u5165\u7684\u79cd\u5b50(seed)\u6765\u786e\u5b9a\u751f\u6210\u968f\u673a\u6570\u7684\u533a\u95f4\uff0c\u53ea\u8981\u79cd\u5b50\u4e00\u6837\uff0c\u83b7\u53d6\u7684\u968f\u673a\u6570\u7684\u5e8f\u5217\u5c31\u662f\u4e00\u81f4\u7684\u3002\u800c\u4e14\u751f\u6210\u7684\u7ed3\u679c\u90fd\u662f\u53ef\u4ee5\u9884\u6d4b\u7684\u3002\u662f\u4e00\u79cd\u4f2a\u968f\u673a\u6570\u7684\u5b9e\u73b0\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u7684\u968f\u673a\u6570\u3002\u6765\u786e\u5b9a\u4f7f\u7528\u7684\u4f46\u662f\u6709\u4e9b\u7528\u4f8b\u76f4\u63a5\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u95ee\u9898\u3002Random\u7684\u4e00\u4e2a\u666e\u904d\u7528\u6cd5\uff1a<\/p>\n
\/\/ Random \u5b9e\u4f8b \r\nRandom random = new Random(); \r\n\/\/\u8c03\u7528 nextInt() \u65b9\u6cd5 \u6b64\u5916\u8fd8\u6709nextDouble(), nextBoolean(), nextFloat(), ... \r\nrandom.nextInt(); \r\n\u6216\u8005\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528java\u4e2d\u7684\u6570\u5b66\u8ba1\u7b97\u7c7b\uff1a\r\nMath.random(); \r\nMath\u7c7b\u53ea\u5305\u542b\u4e00\u4e2aRandom\u5b9e\u4f8b\u6765\u751f\u6210\u968f\u673a\u6570\uff1a\r\npublic static double random() { \r\n Random rnd = randomNumberGenerator; \r\n if (rnd == null) \uff5b \r\n \/\/ \u8fd4\u56de\u4e00\u4e2a\u65b0\u7684Random\u5b9e\u4f8b \r\n rnd = initRNG(); \r\n \uff5d \r\n return rnd.nextDouble(); \r\n } \r\n<\/pre>\njava.util.Random\u7684\u7528\u6cd5\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u4f46\u662f\uff0c\u5728\u4e0d\u540c\u7ebf\u7a0b\u4e0a\u5e76\u53d1\u4f7f\u7528\u76f8\u540c\u7684Random\u5b9e\u4f8b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e89\u7528\uff0c\u4ece\u800c\u5bfc\u81f4\u6027\u80fd\u4e0d\u4f73\u3002\u5176\u539f\u56e0\u662f\u4f7f\u7528\u6240\u8c13\u7684\u79cd\u5b50\u6765\u751f\u6210\u968f\u673a\u6570\u3002\u79cd\u5b50\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u5b57\uff0c\u5b83\u4e3a\u751f\u6210\u65b0\u7684\u968f\u673a\u6570\u63d0\u4f9b\u4e86\u57fa\u7840\u3002\u6211\u4eec\u6765\u770b\u770bRandom\u4e2d\u7684next(int bits)\u65b9\u6cd5\uff1a<\/p>\n
protected int next(int bits) { \r\n long oldseed, nextseed; \r\n AtomicLong seed = this.seed; \r\n do { \r\n oldseed = seed.get(); \r\n nextseed = (oldseed * multiplier addend) & mask; \r\n } while (!seed.compareAndSet(oldseed, nextseed)); \r\n return (int)(nextseed >>> (48 - bits));} \r\n<\/pre>\n\u9996\u5148\uff0c\u65e7\u79cd\u5b50\u548c\u65b0\u79cd\u5b50\u5b58\u50a8\u5728\u4e24\u4e2a\u8f85\u52a9\u53d8\u91cf\u4e0a\u3002\u5728\u8fd9\u4e00\u70b9\u4e0a\uff0c\u521b\u9020\u65b0\u79cd\u5b50\u7684\u539f\u5219\u5e76\u4e0d\u91cd\u8981\u3002\u8981\u4fdd\u5b58\u65b0\u79cd\u5b50\uff0c\u4f7f\u7528compareAndSet()\u65b9\u6cd5\u5c06\u65e7\u79cd\u5b50\u66ff\u6362\u4e3a\u4e0b\u4e00\u4e2a\u65b0\u79cd\u5b50\uff0c\u4f46\u8fd9\u4ec5\u4ec5\u5728\u65e7\u79cd\u5b50\u5bf9\u5e94\u4e8e\u5f53\u524d\u8bbe\u7f6e\u7684\u79cd\u5b50\u7684\u6761\u4ef6\u4e0b\u624d\u4f1a\u89e6\u53d1\u3002\u5982\u679c\u6b64\u65f6\u7684\u503c\u7531\u5e76\u53d1\u7ebf\u7a0b\u64cd\u7eb5\uff0c\u5219\u8be5\u65b9\u6cd5\u8fd4\u56defalse\uff0c\u8fd9\u610f\u5473\u7740\u65e7\u503c\u4e0e\u4f8b\u5916\u503c\u4e0d\u5339\u914d\u3002\u56e0\u4e3a\u662f\u5faa\u73af\u5185\u8fdb\u884c\u7684\u64cd\u4f5c\uff0c\u90a3\u4e48\u4f1a\u53d1\u751f\u81ea\u65cb\uff0c\u76f4\u5230\u53d8\u91cf\u4e0e\u4f8b\u5916\u503c\u5339\u914d\u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0d\u4f73\u548c\u7ebf\u7a0b\u7ade\u4e89\u3002<\/p>\n
\u591a\u7ebf\u7a0b\u4e0b\u7684\u968f\u673a\u6570<\/strong><\/span><\/div>\n\u5982\u679c\u66f4\u591a\u7ebf\u7a0b\u4e3b\u52a8\u751f\u6210\u5177\u6709\u76f8\u540cRandom\u7684\u5b9e\u4f8b\u7684\u65b0\u968f\u673a\u6570\uff0c\u5219\u4e0a\u8ff0\u60c5\u51b5\u53d1\u751f\u7684\u6982\u7387\u8d8a\u9ad8\u3002\u5bf9\u4e8e\u751f\u6210\u8bb8\u591a(\u975e\u5e38\u591a)\u968f\u673a\u6570\u7684\u7a0b\u5e8f\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528ThreadLocalRandom\uff0c\u5b83\u57281.7\u7248\u672c\u4e2d\u6dfb\u52a0\u5230Java\u4e2d\u3002ThreadLocalRandom\u6269\u5c55\u4e86Random\u5e76\u6dfb\u52a0\u9009\u9879\u4ee5\u9650\u5236\u5176\u4f7f\u7528\u5230\u76f8\u5e94\u7684\u7ebf\u7a0b\u5b9e\u4f8b\u3002\u4e3a\u6b64\uff0cThreadLocalRandom\u7684\u5b9e\u4f8b\u4fdd\u5b58\u5728\u76f8\u5e94\u7ebf\u7a0b\u7684\u5185\u90e8\u6620\u5c04\u4e2d\uff0c\u5e76\u901a\u8fc7\u8c03\u7528current()\u6765\u8fd4\u56de\u5bf9\u5e94\u7684Random\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n
ThreadLocalRandom.current().nextInt() \r\n<\/pre>\n\u5b89\u5168\u7684\u968f\u673a\u6570<\/strong><\/span><\/div>\n\u901a\u8fc7\u5bf9Random\u7684\u4e00\u4e9b\u5206\u6790\u6211\u4eec\u53ef\u4ee5\u77e5\u9053Random\u4e8b\u5b9e\u4e0a\u662f\u4f2a\u968f\u673a\uff0c\u662f\u53ef\u4ee5\u63a8\u5bfc\u51fa\u89c4\u5f8b\u7684\uff0c\u800c\u4e14\u4f9d\u8d56\u79cd\u5b50(seed)\u3002\u5982\u679c\u6211\u4eec\u641e\u62bd\u5956\u6216\u8005\u5176\u4ed6\u4e00\u4e9b\u5bf9\u968f\u673a\u6570\u654f\u611f\u7684\u573a\u666f\u65f6\uff0c\u7528Random\u5c31\u4e0d\u5408\u9002\u4e86\uff0c\u5bb9\u6613\u88ab\u4eba\u94bb\u7a7a\u5b50\u3002JDK\u63d0\u4f9b\u4e86SecureRandom\u6765\u89e3\u51b3\u8fd9\u4e2a\u4e8b\u60c5\u3002
\nSecureRandom\u662f\u5f3a\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u5b83\u53ef\u4ee5\u4ea7\u751f\u9ad8\u5f3a\u5ea6\u7684\u968f\u673a\u6570\uff0c\u4ea7\u751f\u9ad8\u5f3a\u5ea6\u7684\u968f\u673a\u6570\u4f9d\u8d56\u4e24\u4e2a\u91cd\u8981\u7684\u56e0\u7d20\uff1a\u79cd\u5b50\u548c\u7b97\u6cd5\u3002\u7b97\u6cd5\u662f\u53ef\u4ee5\u6709\u5f88\u591a\u7684\uff0c\u901a\u5e38\u5982\u4f55\u9009\u62e9\u79cd\u5b50\u662f\u975e\u5e38\u5173\u952e\u7684\u56e0\u7d20\u3002 Random\u7684\u79cd\u5b50\u662fSystem.currentTimeMillis()\uff0c\u6240\u4ee5\u5b83\u7684\u968f\u673a\u6570\u90fd\u662f\u53ef\u9884\u6d4b\u7684\uff0c \u662f\u5f31\u4f2a\u968f\u673a\u6570\u3002\u5f3a\u4f2a\u968f\u673a\u6570\u7684\u751f\u6210\u601d\u8def\uff1a\u6536\u96c6\u8ba1\u7b97\u673a\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u952e\u76d8\u8f93\u5165\u65f6\u95f4\uff0c\u5185\u5b58\u4f7f\u7528\u72b6\u6001\uff0c\u786c\u76d8\u7a7a\u95f2\u7a7a\u95f4\uff0cIO\u5ef6\u65f6\uff0c\u8fdb\u7a0b\u6570\u91cf\uff0c\u7ebf\u7a0b\u6570\u91cf\u7b49\u4fe1\u606f\uff0cCPU\u65f6\u949f\uff0c\u6765\u5f97\u5230\u4e00\u4e2a\u8fd1\u4f3c\u968f\u673a\u7684\u79cd\u5b50\uff0c\u4e3b\u8981\u662f\u8fbe\u5230\u4e0d\u53ef\u9884\u6d4b\u6027\u3002\u8bf4\u7684\u66f4\u901a\u4fd7\u5c31\u662f\uff0c\u4f7f\u7528\u52a0\u5bc6\u7b97\u6cd5\u751f\u6210\u5f88\u957f\u7684\u4e00\u4e2a\u968f\u673a\u79cd\u5b50\uff0c\u8ba9\u4f60\u65e0\u6cd5\u731c\u6d4b\u51fa\u79cd\u5b50\uff0c\u4e5f\u5c31\u65e0\u6cd5\u63a8\u5bfc\u51fa\u968f\u673a\u5e8f\u5217\u6570\u3002<\/p>\n
\u603b\u7ed3<\/strong><\/span><\/div>\n\u4eca\u5929\u6211\u4eec\u63a2\u8ba8\u4e86\u4e1a\u52a1\u4e2d\u7ecf\u5e38\u4f7f\u7528\u7684\u968f\u673a\u6570\u7684\u4e00\u4e9b\u673a\u5236\u548c\u4e00\u4e9b\u573a\u666f\u4e0b\u7684\u4e00\u4e9b\u9677\u9631\uff0c\u5e0c\u671b\u4f60\u5728\u4f7f\u7528\u968f\u673a\u6570\u7684\u65f6\u5019\u80fd\u907f\u514d\u8fd9\u79cd\u9677\u9631\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u968f\u673a\u6570\u6211\u4eec\u5e94\u8be5\u4e0d\u964c\u751f\uff0c\u4e1a\u52a1\u4e2d\u6211\u4eec\u7528\u5b83\u6765\u751f\u6210\u9a8c\u8bc1\u7801\uff0c\u6216\u8005\u5bf9\u91cd\u590d\u6027\u8981\u6c42\u4e0d\u9ad8\u7684id\uff0c\u751a\u81f3\u6211\u4eec\u8fd8\u7528\u5b83\u5728\u5e74\u4f1a\u4e0a\u641e\u62bd\u5956\u3002\u4eca […]<\/p>\n","protected":false},"author":1898,"featured_media":160765,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[205],"class_list":["post-160763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-java"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=160763"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160763\/revisions"}],"predecessor-version":[{"id":160797,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160763\/revisions\/160797"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/160765"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=160763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=160763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=160763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}