{"id":2424,"date":"2015-08-19T22:07:53","date_gmt":"2015-08-19T13:07:53","guid":{"rendered":"http:\/\/taitan916.info\/blog\/?p=2424"},"modified":"2024-01-28T18:53:24","modified_gmt":"2024-01-28T09:53:24","slug":"post-2424","status":"publish","type":"post","link":"https:\/\/taitan916.info\/blog\/archives\/2424","title":{"rendered":"\u30bd\u30b7\u30e3\u30b2\u306e\u30ac\u30c1\u30e3\u306e\u3088\u3046\u306b\u5f53\u9078\u78ba\u7387\u304c\u7570\u306a\u308b\u62bd\u9078\u30b7\u30b9\u30c6\u30e0\u3092PHP\u3067\u4f5c\u6210\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>\u30bd\u30b7\u30e3\u30b2\u306e\u30ac\u30c1\u30e3\u307f\u305f\u3044\u306a\u5f53\u9078\u78ba\u7387\u304c\u7570\u306a\u308b\u62bd\u9078\u30b7\u30b9\u30c6\u30e0\u3092PHP\u3067\u4f5c\u6210\u3057\u305f\u304b\u3063\u305f\u3002\u5177\u4f53\u7684\u306b\u306f\u30ec\u30a2\u5ea6\u304c1\uff5e6\u307e\u3067\u3042\u308a\u3001\u30ec\u30a2\u5ea6\u304c\u4f4e\u3044\u3082\u306e\u306f\u5f53\u305f\u308a\u3084\u3059\u304f\u30ec\u30a2\u5ea6\u304c\u9ad8\u3044\u3082\u306e\u306f\u5f53\u305f\u308a\u306b\u304f\u3044\u3068\u3044\u3046\u3082\u306e\u3002\u307e\u305f\u3001\u30ec\u30a2\u5ea6\u305d\u308c\u305e\u308c\u306e\u5f53\u305f\u308a\u3084\u3059\u3055(\u5f53\u9078\u78ba\u7387)\u3082\u5236\u5fa1\u3057\u305f\u3044\u3002\u4ee5\u4e0b\u306b\u5b9f\u88c5\u65b9\u6cd5\u3092\u30e1\u30e2\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>\u30bd\u30fc\u30b9<\/strong><\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\n\/\/\u30ac\u30c1\u30e3\u7528\u30c7\u30fc\u30bf\r\n$data = array(\r\n    0 =&gt; array('name' =&gt; '\u661f1', 'rate' =&gt; 30), \r\n    1 =&gt; array('name' =&gt; '\u661f2', 'rate' =&gt; 25), \r\n    2 =&gt; array('name' =&gt; '\u661f3', 'rate' =&gt; 20), \r\n    3 =&gt; array('name' =&gt; '\u661f4', 'rate' =&gt; 15), \r\n    4 =&gt; array('name' =&gt; '\u661f5', 'rate' =&gt; 9), \r\n    5 =&gt; array('name' =&gt; '\u661f6', 'rate' =&gt; 1), \r\n);\r\necho $data[gacha($data)]['name']; \/\/\u5f53\u305f\u3063\u305f\u914d\u5217\u306ename\u3092\u51fa\u529b\r\n\r\n\r\nfunction gacha( $data = array() ){\r\n\r\n    \/\/\u78ba\u7387\u306e\u5408\u8a08\u5024\r\n    $max = 0;\r\n    foreach( $data as $record ) $max += $record['rate'];\r\n\r\n    \/\/\u5f53\u305f\u308a\u306e\u6c7a\u5b9a\r\n    $hit = mt_rand( 0, ($max - 1) );\r\n\r\n    \/\/\u5f53\u305f\u3063\u305f\u4e71\u6570\u304c\u3069\u306e\u90e8\u5206\u306b\u306a\u308b\u306e\u304b\u8abf\u3079\u3066\u30d2\u30c3\u30c8\u3057\u305f\u3089\u8fd4\u3059\r\n    foreach( $data as $key =&gt; $record ){\r\n        $max -= $record['rate'];\r\n        if( $max &lt;= $hit ) return $key;\r\n    }\r\n\r\n}\r\n?&gt;<\/pre>\n<h3><strong>\u5f53\u9078\u78ba\u7387\u306b\u3064\u3044\u3066<\/strong><\/h3>\n<p>rate\u304c\u78ba\u7387\u306e\u90e8\u5206\u3002\u6574\u6570\u5024\u3067\u3082\u5c0f\u6570\u5024\u3067\u3082\u826f\u3044\u3002\u5408\u8a08\u3092100\u306b\u3059\u308b\u3068\u30d1\u30fc\u30bb\u30f3\u30c6\u30fc\u30b8\u3068\u3057\u3066\u5206\u304b\u308a\u3084\u3059\u3044\u3002100\u3092\u8d85\u3048\u3066\u3082\u554f\u984c\u306a\u3044\u3002<\/p>\n<h3>\u5b9f\u904b\u7528\u306b\u3064\u3044\u3066<\/h3>\n<p>\u5b9f\u904b\u7528\u3068\u3057\u3066\u306f\u4e88\u3081MySQL\u306b\u30ec\u30a2\u5ea61\uff5e6\u306e\u30ac\u30c1\u30e3\u5546\u54c1(\u30a2\u30a4\u30c6\u30e0\u3084\u30ad\u30e3\u30e9\u30af\u30bf\u7b49)\u306e\u30c7\u30fc\u30bf\u3092\u30ec\u30a2\u5ea6\u3068\u5171\u306b\u4fdd\u5b58\u3057\u3066\u304a\u304d\u4e0a\u8a18\u306e\u30ac\u30c1\u30e3\u3067\u62bd\u9078\u5f8c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306aSQL\u6587\u3067\u53d6\u308a\u51fa\u3059\u5f62\u306b\u306a\u308a\u305d\u3046\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/\u6307\u5b9a\u3057\u305f\u30ec\u30a2\u5ea6\u306e\u30c7\u30fc\u30bf\u3092\u30e9\u30f3\u30c0\u30e0\u30671\u4ef6\u53d6\u5f97\r\nSELECT * \r\nFROM `gacha` \r\nWHERE `rate` = '\u30ec\u30a2\u5ea6'\r\nORDER BY RAND() \r\nLIMIT 1;<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>\u305d\u306e\u4ed6<\/strong><\/h2>\n<p>\u6700\u521d\u306f\u78ba\u7387\u5206\u306e\u914d\u5217\u3092\u4f5c\u3063\u3066\u3001\u305d\u3053\u304b\u3089\u30e9\u30f3\u30c0\u30e0\u306b\u5f15\u3051\u3070\u826f\u3044\u306e\u304b\u3068\u601d\u3063\u305f\u3002\u4f8b\u306e\u30bd\u30fc\u30b9\u306e\u5834\u5408\u3060\u3068\u8a08100\u500b\u306e\u5024\u304c\u5165\u3063\u305f\u914d\u5217\u3092\u4f5c\u3063\u3066\u305d\u3053\u304b\u3089\u5f15\u304f\u3068\u3044\u3046\u3082\u306e\u3002\u305f\u3060\u3053\u308c\u3060\u3068\u30c7\u30fc\u30bf\u6570\u304c\u591a\u3044\u5834\u5408\u306b\u8ca0\u8377\u304c\u304b\u304b\u308b\u307f\u305f\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30bd\u30b7\u30e3\u30b2\u306e\u30ac\u30c1\u30e3\u307f\u305f\u3044\u306a\u5f53\u9078\u78ba\u7387\u304c\u7570\u306a\u308b\u62bd\u9078\u30b7\u30b9\u30c6\u30e0\u3092PHP\u3067\u4f5c\u6210\u3057\u305f\u304b\u3063\u305f\u3002\u5177 ... <\/p>\n","protected":false},"author":1,"featured_media":1085,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2424","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/2424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/comments?post=2424"}],"version-history":[{"count":4,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/2424\/revisions"}],"predecessor-version":[{"id":4392,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/2424\/revisions\/4392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media\/1085"}],"wp:attachment":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media?parent=2424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/categories?post=2424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/tags?post=2424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}