{"id":9616,"date":"2012-04-23T08:00:42","date_gmt":"2012-04-22T23:00:42","guid":{"rendered":"http:\/\/www.sssg.org\/blogs\/hiro345\/?p=9616"},"modified":"2012-04-23T19:21:03","modified_gmt":"2012-04-23T10:21:03","slug":"dart-structured-web-apps-8-rand","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/9616.html","title":{"rendered":"Dart: Structured web apps [8] random"},"content":{"rendered":"<p>Dart\u306e\u4e71\u6570\u751f\u6210\u306b\u3064\u3044\u3066\u8abf\u3079\u3066\u307f\u307e\u3057\u305f\u3002Math.random() \u3092\u4f7f\u3048\u3070\u826f\u3044\u3088\u3046\u3067\u3059\u304c\u3001\u81ea\u5206\u3067\u5b9f\u88c5\u3057\u305f\u65b9\u304c\u826f\u3044\u5834\u5408\u3082\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br \/>\n<!--more--><br \/>\n\u30b5\u30a4\u30b3\u30ed\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u3068\u601d\u3046\u306e\u3067\u30011\u304b\u30896\u307e\u3067\u306e\u4e71\u6570\u30921000\u56de\u751f\u6210\u3057\u3066\u3001\u305d\u308c\u305e\u308c\u304c\u3069\u308c\u304f\u3089\u3044\u767a\u751f\u3057\u305f\u306e\u304b\u3092\u30ab\u30a6\u30f3\u30c8\u3057\u3066\u307f\u307e\u3057\u305f\u3002RandIntImple1, RandIntImple2 \u306fDart\u306eMath.random()\u3092\u4f7f\u3063\u30660.0\u4ee5\u4e0a 0.1\u672a\u6e80\u306edouble\u578b\u5024\uff08API\u306b\u3088\u308b\u3068\u3001Returns a random double greater than or equal to 0.0 and less than 1.0.\uff09\u3092\u51fa\u3057\u3066\u304b\u3089\u3001\u305d\u306e\u5f8c\u306e\u8a08\u7b97\u3092\u5909\u3048\u3066\u3044\u307e\u3059\u3002RandIntImple3\u306f\u3088\u304f\u3042\u308b\u7dda\u5f62\u5408\u540c\u6cd5\u306b\u3088\u308b\u3082\u306e\u3067\u3001RandIntImple4\u306fxorshift\u306b\u3088\u308b\u3082\u306e\u3067\u3059\u3002RandIntImple3, RandIntImple4 \u306e\u8a08\u7b97\u306b\u306f\u6642\u9593\u304c\u304b\u304b\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: java; gutter: true\">\r\ninterface RandInt {\r\n  int random();\r\n}\r\nclass RandIntImple1 implements RandInt {\r\n  int min = 0;\r\n  int max = 1;\r\n  RandIntImple1() {\r\n  }\r\n  RandIntImple1.minmax(this.min, this.max);\r\n  int random() {\r\n    double r = Math.random() * (max - min + 1) + min;\r\n    return r.toInt();\r\n  }\r\n}\r\nclass RandIntImple2 extends RandIntImple1 implements RandInt {\r\n  RandIntImple2.minmax(int min, int max) : super.minmax(min, max);\r\n  int random() {\r\n    double r = min + (Math.random() * max).floor();\r\n    return r.toInt();\r\n  }\r\n}\r\n\/\/ rand\r\nclass RandIntImple3 extends RandIntImple1 implements RandInt {\r\n  RandIntImple3.minmax(int min, int max) : super.minmax(min, max);\r\n  static num x = 1;\r\n  void srand(num s) {\r\n    x = s;\r\n  }\r\n  num rand() {\r\n    x = x * 1103515245 + 12345;\r\n    return x &amp; 2147483647; \r\n  }\r\n  int random() {\r\n    num r = rand();\r\n    return (min + r) % max + min;\r\n  }\r\n}\r\nclass RandIntImple4 extends RandIntImple1 implements RandInt {\r\n  static num x = 123456789;\r\n  static num y = 362436069;\r\n  static num z = 521288629;\r\n  static num w = 88675123; \r\n  RandIntImple4.minmax(int min, int max) : super.minmax(min, max);\r\n  void seed(num x0, num y0, num z0, num w0) {\r\n    x = x0;\r\n    y = y0;\r\n    z = z0;\r\n    w = w0;\r\n    if (x+y+z+w &lt;= 0) {\r\n      x = 123456789;\r\n      y = 362436069;\r\n      z = 521288629;\r\n      w = 88675123; \r\n    }\r\n  }\r\n  int random() {\r\n    num r = xor128();\r\n    return (min + r) % max + min;\r\n  }\r\n  num xor128(){ \r\n    num t; \r\n    t = (x^(x&lt;&lt;11));\r\n    x = y;\r\n    y = z;\r\n    z = w;\r\n    w = (w^(w&gt;&gt;19))^(t^(t&gt;&gt;8));\r\n    return w; \r\n  }\r\n}\r\n\r\nvoid printRand(RandInt app) {\r\n  List result = new List(7);\r\n  for (int i=0 ; i&lt;result.length ; i++) {\r\n    result[i] = 0;\r\n  }\r\n  for (int i=0 ; i&lt;10000 ; i++) {\r\n    int x = app.random();\r\n    result[x]++;\r\n  }\r\n  result.forEach((e) =&gt; print(e));\r\n  int sum = 0;\r\n  result.forEach((e) =&gt; sum += e);\r\n  print(sum\/6);\r\n}\r\nvoid main() {\r\n  printRand(new RandIntImple1.minmax(1, 6));\r\n  print(&quot;-----&quot;);\r\n  printRand(new RandIntImple2.minmax(1, 6));\r\n  print(&quot;-----&quot;);\r\n  RandIntImple3 rand3 = new RandIntImple3.minmax(1, 6);\r\n  int seed = (new Date.now()).value;\r\n  rand3.srand(seed);\r\n  printRand(rand3);\r\n  print(&quot;-----&quot;);\r\n  printRand(new RandIntImple4.minmax(1, 6));\r\n  print(&quot;-----&quot;);\r\n  RandIntImple4 rand4 = new RandIntImple4.minmax(1, 6);\r\n  rand4.seed(1, 10, 100, 1000);\r\n  printRand(rand4);\r\n}\r\n<\/pre>\n<ul>\n<li>\u30af\u30e9\u30b9\u30d9\u30fc\u30b9\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u57fa\u790e\u3092\u5b66\u3076\u306b\u306f &#8230; <a type=\"amzn\" asin=\"484432523X\">\u6539\u8a02\u7248 \u57fa\u790eJava(CD-ROM\u4ed8) (IMPRESS KISO SERIES)<\/a><\/li>\n<li>\u30af\u30e9\u30b9\u30d9\u30fc\u30b9\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3092\u5b66\u3076\u306b\u306f &#8230; <a type=\"amzn\" asin=\"4894717166\">\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9eJava (The Java Series)<\/a><\/li>\n<li>\u95a2\u6570\u578b\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3092\u5b66\u3076\u306b\u306f &#8230; <a type=\"amzn\" asin=\"4844330845\">Scala\u30b9\u30b1\u30fc\u30e9\u30d6\u30eb\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u7b2c2\u7248<\/a><\/li>\n<li>\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u7406\u8ad6\u3092\u5b66\u306b\u306f &#8230; <a type=\"amzn\" asin=\"4781912850\">\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u57fa\u790e\u6982\u5ff5 (\u30e9\u30a4\u30d6\u30e9\u30ea\u60c5\u5831\u5b66\u30b3\u30a2\u30fb\u30c6\u30ad\u30b9\u30c8) <\/a><\/li>\n<\/ul>\n<p><iframe src=\"\/\/rcm-jp.amazon.co.jp\/e\/cm?t=hiro345-22&#038;o=9&#038;p=8&#038;l=as1&#038;asins=484432523X&#038;ref=tf_til&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><iframe src=\"\/\/rcm-jp.amazon.co.jp\/e\/cm?t=hiro345-22&#038;o=9&#038;p=8&#038;l=as1&#038;asins=4894717166&#038;ref=tf_til&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><iframe src=\"\/\/rcm-jp.amazon.co.jp\/e\/cm?t=hiro345-22&#038;o=9&#038;p=8&#038;l=as1&#038;asins=4844330845&#038;ref=tf_til&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><iframe src=\"\/\/rcm-jp.amazon.co.jp\/e\/cm?t=hiro345-22&#038;o=9&#038;p=8&#038;l=as1&#038;asins=4781912850&#038;ref=tf_til&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dart\u306e\u4e71\u6570\u751f\u6210\u306b\u3064\u3044\u3066\u8abf\u3079\u3066\u307f\u307e\u3057\u305f\u3002Math.random() \u3092\u4f7f\u3048\u3070\u826f\u3044\u3088\u3046\u3067\u3059\u304c\u3001\u81ea\u5206\u3067\u5b9f\u88c5\u3057\u305f\u65b9\u304c\u826f\u3044\u5834\u5408\u3082\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[638,684],"class_list":["post-9616","post","type-post","status-publish","format-standard","hentry","category-program","tag-dart","tag-dartlang"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/9616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=9616"}],"version-history":[{"count":10,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/9616\/revisions"}],"predecessor-version":[{"id":9838,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/9616\/revisions\/9838"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=9616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=9616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=9616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}