{"id":38263,"date":"2024-11-26T16:21:54","date_gmt":"2024-11-26T08:21:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38263\/"},"modified":"2024-11-26T16:21:54","modified_gmt":"2024-11-26T08:21:54","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e4%ba%a7%e7%94%9f%e9%9a%8f%e6%9c%ba%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38263\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u4ea7\u751f\u968f\u673a\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u751f\u6210\u968f\u673a\u6570\u7ec4\u53ef\u4f7f\u7528 math.random() \u65b9\u6cd5\u6216 random \u7c7b\u3002math.random() \u65b9\u6cd5\u53ef\u751f\u6210 0.0 \u81f3 1.0 \u4e4b\u95f4\u7684\u968f\u673a\u6570\uff0c\u800c random \u7c7b\u63d0\u4f9b\u4e86\u66f4\u591a\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u5305\u62ec nextint() \u548c nextdouble()\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111411452816694.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u4ea7\u751f\u968f\u673a\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u4ea7\u751f\u968f\u673a\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u751f\u6210\u968f\u673a\u6570\u7ec4\u7684\u65b9\u6cd5<\/strong><\/p>\n<p><strong>1. \u4f7f\u7528 Math.random() \u65b9\u6cd5<\/strong><\/p>\n<p>Math.random() \u65b9\u6cd5\u53ef\u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e 0.0\uff08\u5305\u62ec\uff09\u548c 1.0\uff08\u4e0d\u5305\u62ec\uff09\u4e4b\u95f4\u7684\u968f\u673a\u6570\u3002\u8981\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u3002<\/li>\n<li>\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n<li>\u4e3a\u6bcf\u4e2a\u5143\u7d20\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.Random;\n\npublic class RandomArray {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a 10 \u7684 int \u6570\u7ec4\n        int[] arr = new int[10];\n\n        \/\/ \u904d\u5386\u6570\u7ec4\u5e76\u751f\u6210\u968f\u673a\u6570\n        Random random = new Random();\n        for (int i = 0; i &lt; arr.length; i++) {\n            arr[i] = random.nextInt();\n        }\n\n        \/\/ \u8f93\u51fa\u6570\u7ec4\n        System.out.println(\"\u968f\u673a\u6570\u7ec4\uff1a\");\n        for (int num : arr) {\n            System.out.println(num);\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Random \u7c7b<\/strong><\/p>\n<p>Random \u7c7b\u63d0\u4f9b\u4e86\u66f4\u591a\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u3002\u8981\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a Random \u5bf9\u8c61\u3002<\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u3002<\/li>\n<li>\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n<li>\u4f7f\u7528 Random \u5bf9\u8c61\u7684 nextInt() \u6216 nextDouble() \u65b9\u6cd5\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.Random;\n\npublic class RandomArray {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2a Random \u5bf9\u8c61\n        Random random = new Random();\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a 10 \u7684 int \u6570\u7ec4\n        int[] arr = new int[10];\n\n        \/\/ \u904d\u5386\u6570\u7ec4\u5e76\u751f\u6210\u968f\u673a\u6570\n        for (int i = 0; i &lt; arr.length; i++) {\n            arr[i] = random.nextInt();\n        }\n\n        \/\/ \u8f93\u51fa\u6570\u7ec4\n        System.out.println(\"\u968f\u673a\u6570\u7ec4\uff1a\");\n        for (int num : arr) {\n            System.out.println(num);\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u4ea7\u751f\u968f\u673a\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u751f\u6210\u968f\u673a\u6570\u7ec4\u53ef\u4f7f\u7528 math.random() \u65b9\u6cd5\u6216 random \u7c7b\u3002math.random() \u65b9\u6cd5\u53ef\u751f\u6210 0.0 \u81f3 1.0 \u4e4b\u95f4\u7684\u968f\u673a\u6570\uff0c\u800c random \u7c7b\u63d0\u4f9b\u4e86\u66f4\u591a\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\uff0c\u5305\u62ec nextint() \u548c nextdouble()\u3002 Java \u4e2d\u751f\u6210\u968f\u673a\u6570\u7ec4\u7684\u65b9\u6cd5 1. \u4f7f\u7528 Math.random() \u65b9\u6cd5 Math.random() \u65b9\u6cd5\u53ef\u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e 0.0\uff08\u5305\u62ec\uff09\u548c 1.0\uff08\u4e0d\u5305\u62ec\uff09\u4e4b\u95f4\u7684\u968f\u673a\u6570\u3002\u8981\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u3002 \u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u3002 \u4e3a\u6bcf\u4e2a\u5143\u7d20\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import java.util.Random; public class RandomArray { public static void main(String[] args) { \/\/ \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a 10 \u7684 int \u6570\u7ec4 int[] arr = new int[10]; \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-38263","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38263"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38263\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}