browserified.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. },{}],2:[function(require,module,exports){
  3. arguments[4][1][0].apply(exports,arguments)
  4. },{"dup":1}],3:[function(require,module,exports){
  5. /*
  6. Copyright 2014 David Bau.
  7. Permission is hereby granted, free of charge, to any person obtaining
  8. a copy of this software and associated documentation files (the
  9. "Software"), to deal in the Software without restriction, including
  10. without limitation the rights to use, copy, modify, merge, publish,
  11. distribute, sublicense, and/or sell copies of the Software, and to
  12. permit persons to whom the Software is furnished to do so, subject to
  13. the following conditions:
  14. The above copyright notice and this permission notice shall be
  15. included in all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. (function (pool, math) {
  25. //
  26. // The following constants are related to IEEE 754 limits.
  27. //
  28. var global = this,
  29. width = 256, // each RC4 output is 0 <= x < 256
  30. chunks = 6, // at least six RC4 outputs for each double
  31. digits = 52, // there are 52 significant digits in a double
  32. rngname = 'random', // rngname: name for Math.random and Math.seedrandom
  33. startdenom = math.pow(width, chunks),
  34. significance = math.pow(2, digits),
  35. overflow = significance * 2,
  36. mask = width - 1,
  37. nodecrypto; // node.js crypto module, initialized at the bottom.
  38. //
  39. // seedrandom()
  40. // This is the seedrandom function described above.
  41. //
  42. function seedrandom(seed, options, callback) {
  43. var key = [];
  44. options = (options == true) ? { entropy: true } : (options || {});
  45. // Flatten the seed string or build one from local entropy if needed.
  46. var shortseed = mixkey(flatten(
  47. options.entropy ? [seed, tostring(pool)] :
  48. (seed == null) ? autoseed() : seed, 3), key);
  49. // Use the seed to initialize an ARC4 generator.
  50. var arc4 = new ARC4(key);
  51. // This function returns a random double in [0, 1) that contains
  52. // randomness in every bit of the mantissa of the IEEE 754 value.
  53. var prng = function() {
  54. var n = arc4.g(chunks), // Start with a numerator n < 2 ^ 48
  55. d = startdenom, // and denominator d = 2 ^ 48.
  56. x = 0; // and no 'extra last byte'.
  57. while (n < significance) { // Fill up all significant digits by
  58. n = (n + x) * width; // shifting numerator and
  59. d *= width; // denominator and generating a
  60. x = arc4.g(1); // new least-significant-byte.
  61. }
  62. while (n >= overflow) { // To avoid rounding up, before adding
  63. n /= 2; // last byte, shift everything
  64. d /= 2; // right using integer math until
  65. x >>>= 1; // we have exactly the desired bits.
  66. }
  67. return (n + x) / d; // Form the number within [0, 1).
  68. };
  69. prng.int32 = function() { return arc4.g(4) | 0; }
  70. prng.quick = function() { return arc4.g(4) / 0x100000000; }
  71. prng.double = prng;
  72. // Mix the randomness into accumulated entropy.
  73. mixkey(tostring(arc4.S), pool);
  74. // Calling convention: what to return as a function of prng, seed, is_math.
  75. return (options.pass || callback ||
  76. function(prng, seed, is_math_call, state) {
  77. if (state) {
  78. // Load the arc4 state from the given state if it has an S array.
  79. if (state.S) { copy(state, arc4); }
  80. // Only provide the .state method if requested via options.state.
  81. prng.state = function() { return copy(arc4, {}); }
  82. }
  83. // If called as a method of Math (Math.seedrandom()), mutate
  84. // Math.random because that is how seedrandom.js has worked since v1.0.
  85. if (is_math_call) { math[rngname] = prng; return seed; }
  86. // Otherwise, it is a newer calling convention, so return the
  87. // prng directly.
  88. else return prng;
  89. })(
  90. prng,
  91. shortseed,
  92. 'global' in options ? options.global : (this == math),
  93. options.state);
  94. }
  95. math['seed' + rngname] = seedrandom;
  96. //
  97. // ARC4
  98. //
  99. // An ARC4 implementation. The constructor takes a key in the form of
  100. // an array of at most (width) integers that should be 0 <= x < (width).
  101. //
  102. // The g(count) method returns a pseudorandom integer that concatenates
  103. // the next (count) outputs from ARC4. Its return value is a number x
  104. // that is in the range 0 <= x < (width ^ count).
  105. //
  106. function ARC4(key) {
  107. var t, keylen = key.length,
  108. me = this, i = 0, j = me.i = me.j = 0, s = me.S = [];
  109. // The empty key [] is treated as [0].
  110. if (!keylen) { key = [keylen++]; }
  111. // Set up S using the standard key scheduling algorithm.
  112. while (i < width) {
  113. s[i] = i++;
  114. }
  115. for (i = 0; i < width; i++) {
  116. s[i] = s[j = mask & (j + key[i % keylen] + (t = s[i]))];
  117. s[j] = t;
  118. }
  119. // The "g" method returns the next (count) outputs as one number.
  120. (me.g = function(count) {
  121. // Using instance members instead of closure state nearly doubles speed.
  122. var t, r = 0,
  123. i = me.i, j = me.j, s = me.S;
  124. while (count--) {
  125. t = s[i = mask & (i + 1)];
  126. r = r * width + s[mask & ((s[i] = s[j = mask & (j + t)]) + (s[j] = t))];
  127. }
  128. me.i = i; me.j = j;
  129. return r;
  130. // For robust unpredictability, the function call below automatically
  131. // discards an initial batch of values. This is called RC4-drop[256].
  132. // See http://google.com/search?q=rsa+fluhrer+response&btnI
  133. })(width);
  134. }
  135. //
  136. // copy()
  137. // Copies internal state of ARC4 to or from a plain object.
  138. //
  139. function copy(f, t) {
  140. t.i = f.i;
  141. t.j = f.j;
  142. t.S = f.S.slice();
  143. return t;
  144. };
  145. //
  146. // flatten()
  147. // Converts an object tree to nested arrays of strings.
  148. //
  149. function flatten(obj, depth) {
  150. var result = [], typ = (typeof obj), prop;
  151. if (depth && typ == 'object') {
  152. for (prop in obj) {
  153. try { result.push(flatten(obj[prop], depth - 1)); } catch (e) {}
  154. }
  155. }
  156. return (result.length ? result : typ == 'string' ? obj : obj + '\0');
  157. }
  158. //
  159. // mixkey()
  160. // Mixes a string seed into a key that is an array of integers, and
  161. // returns a shortened string seed that is equivalent to the result key.
  162. //
  163. function mixkey(seed, key) {
  164. var stringseed = seed + '', smear, j = 0;
  165. while (j < stringseed.length) {
  166. key[mask & j] =
  167. mask & ((smear ^= key[mask & j] * 19) + stringseed.charCodeAt(j++));
  168. }
  169. return tostring(key);
  170. }
  171. //
  172. // autoseed()
  173. // Returns an object for autoseeding, using window.crypto and Node crypto
  174. // module if available.
  175. //
  176. function autoseed() {
  177. try {
  178. var out;
  179. if (nodecrypto && (out = nodecrypto.randomBytes)) {
  180. // The use of 'out' to remember randomBytes makes tight minified code.
  181. out = out(width);
  182. } else {
  183. out = new Uint8Array(width);
  184. (global.crypto || global.msCrypto).getRandomValues(out);
  185. }
  186. return tostring(out);
  187. } catch (e) {
  188. var browser = global.navigator,
  189. plugins = browser && browser.plugins;
  190. return [+new Date, global, plugins, global.screen, tostring(pool)];
  191. }
  192. }
  193. //
  194. // tostring()
  195. // Converts an array of charcodes to a string
  196. //
  197. function tostring(a) {
  198. return String.fromCharCode.apply(0, a);
  199. }
  200. //
  201. // When seedrandom.js is loaded, we immediately mix a few bits
  202. // from the built-in RNG into the entropy pool. Because we do
  203. // not want to interfere with deterministic PRNG state later,
  204. // seedrandom will not call math.random on its own again after
  205. // initialization.
  206. //
  207. mixkey(math.random(), pool);
  208. //
  209. // Nodejs and AMD support: export the implementation as a module using
  210. // either convention.
  211. //
  212. if ((typeof module) == 'object' && module.exports) {
  213. module.exports = seedrandom;
  214. // When in node.js, try using crypto package for autoseeding.
  215. try {
  216. nodecrypto = require('crypto');
  217. } catch (ex) {}
  218. } else if ((typeof define) == 'function' && define.amd) {
  219. define(function() { return seedrandom; });
  220. }
  221. // End anonymous scope, and pass initial values.
  222. })(
  223. [], // pool: entropy pool starts empty
  224. Math // math: package containing random, pow, and seedrandom
  225. );
  226. },{"crypto":1}],4:[function(require,module,exports){
  227. (function (__dirname){
  228. var assert = require("assert");
  229. var seedrandom = require("../seedrandom");
  230. var requirejs = require("requirejs");
  231. // Stub out requirejs if in the browser via browserify.
  232. if (!requirejs.config) {
  233. requirejs = require;
  234. } else {
  235. requirejs.config({
  236. baseUrl: __dirname
  237. });
  238. }
  239. describe("Nodejs API Test", function() {
  240. it('should pass basic tests.', function() {
  241. var original = Math.random,
  242. result, r, xprng, obj, as2, as3, autoseed1, myrng,
  243. firstprng, secondprng, thirdprng, rng;
  244. result = Math.seedrandom('hello.');
  245. firstprng = Math.random;
  246. assert.ok(original !== firstprng, "Should change Math.random.");
  247. assert.equal(result, "hello.", "Should return short seed.");
  248. r = Math.random();
  249. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  250. r = Math.random();
  251. assert.equal(r, 0.3752569768646784, "Should be 'hello.'#2");
  252. // should be able to autoseed
  253. result = Math.seedrandom();
  254. secondprng = Math.random;
  255. assert.ok(original !== secondprng, "Should change Math.random.");
  256. assert.ok(firstprng !== secondprng, "Should change Math.random.");
  257. assert.equal(result.length, 256, "Should return short seed.");
  258. r = Math.random();
  259. assert.ok(r > 0, "Should be posititive.");
  260. assert.ok(r < 1, "Should be less than 1.");
  261. assert.ok(r != 0.9282578795792454, "Should not be 'hello.'#1");
  262. assert.ok(r != 0.3752569768646784, "Should not be 'hello.'#2");
  263. assert.ok(r != 0.7316977468919549, "Should not be 'hello.'#3");
  264. autoseed1 = r;
  265. // should be able to add entropy.
  266. result = Math.seedrandom('added entropy.', { entropy:true });
  267. assert.equal(result.length, 256, "Should return short seed.");
  268. thirdprng = Math.random;
  269. assert.ok(thirdprng !== secondprng, "Should change Math.random.");
  270. r = Math.random();
  271. assert.ok(r != 0.597067214994467, "Should not be 'added entropy.'#1");
  272. // Reset to original Math.random.
  273. Math.random = original;
  274. // should be able to use new Math.seedrandom('hello.')
  275. myrng = new Math.seedrandom('hello.');
  276. assert.ok(original === Math.random, "Should not change Math.random.");
  277. assert.ok(original !== myrng, "PRNG should not be Math.random.");
  278. r = myrng();
  279. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  280. // should be able to use seedrandom('hello.')"
  281. rng = seedrandom('hello.');
  282. assert.equal(typeof(rng), 'function', "Should return a function.");
  283. r = rng();
  284. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  285. assert.ok(original === Math.random, "Should not change Math.random.");
  286. assert.ok(original !== rng, "PRNG should not be Math.random.");
  287. // Global PRNG: set Math.random.
  288. // should be able to use seedrandom('hello.', { global: true })
  289. result = seedrandom('hello.', { global: true });
  290. assert.equal(result, 'hello.', "Should return short seed.");
  291. assert.ok(original != Math.random, "Should change Math.random.");
  292. r = Math.random();
  293. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  294. // Autoseeded non-global
  295. Math.random = original;
  296. // should be able to use seedrandom()
  297. result = seedrandom();
  298. assert.equal(typeof(result), 'function', "Should return function.");
  299. assert.ok(original === Math.random, "Should not change Math.random.");
  300. r = result();
  301. // got " + r);
  302. assert.ok(r != autoseed1, "Should not repeat previous autoseed.");
  303. assert.ok(r != 0.9282578795792454, "Should not be 'hello.'#1");
  304. assert.ok(r != 0.7316977468919549, "Should not be 'hello.'#3");
  305. // Mixing accumulated entropy.
  306. // should be able to use seedrandom('added entropy.', { entropy: true })
  307. rng = seedrandom('added entropy.', { entropy: true });
  308. r = result();
  309. // got " + r);
  310. assert.ok(r != autoseed1, "Should not repeat previous autoseed.");
  311. assert.ok(r != 0.597067214994467, "Should not be 'added entropy.'#1");
  312. // Legacy calling convention for mixing accumulated entropy.
  313. // should be able to use seedrandom('added entropy.', true)
  314. rng = seedrandom('added entropy.', true);
  315. r = result();
  316. // got " + r);
  317. assert.ok(r != autoseed1, "Should not repeat previous autoseed.");
  318. assert.ok(r != 0.597067214994467, "Should not be 'added entropy.'#1");
  319. // The pass option
  320. // should be able to use Math.seedrandom(null, { pass: ...
  321. obj = Math.seedrandom(null, { pass: function(prng, seed) {
  322. return { random: prng, seed: seed };
  323. }});
  324. assert.ok(original === Math.random, "Should not change Math.random.");
  325. assert.ok(original !== obj.random, "Should be different from Math.random.");
  326. assert.equal(typeof(obj.random), 'function', "Should return a PRNG function.");
  327. assert.equal(typeof(obj.seed), 'string', "Should return a seed.");
  328. as2 = obj.random();
  329. assert.ok(as2 != 0.9282578795792454, "Should not be 'hello.'#1");
  330. rng = seedrandom(obj.seed);
  331. as3 = rng();
  332. assert.equal(as2, as3, "Should be reproducible when using the seed.");
  333. // Exercise pass again, with explicit seed and global
  334. // should be able to use Math.seedrandom('hello.', { pass: ...
  335. result = Math.seedrandom('hello.', {
  336. global: 'abc',
  337. pass: function(prng, seed, global) {
  338. assert.equal(typeof(prng), 'function', "Callback arg #1 assert");
  339. assert.equal(seed, 'hello.', "Callback arg #2 assert");
  340. assert.equal(global, 'abc', "Callback arg #3 passed through.");
  341. assert.equal(prng(), 0.9282578795792454, "Should be 'hello.'#1");
  342. return 'def';
  343. }});
  344. assert.equal(result, 'def', "Should return value from callback.");
  345. assert.ok(original === Math.random, "Should not change Math.random.");
  346. // Legacy third argument callback argument:
  347. // should be able to use Math.seedrandom('hello.', { global: 50 }, callback)
  348. result = Math.seedrandom('hello.', { global: 50 },
  349. function(prng, seed, global) {
  350. assert.equal(typeof(prng), 'function', "Callback arg #1 assert");
  351. assert.equal(seed, 'hello.', "Callback arg #2 assert");
  352. assert.equal(global, 50, "Callback arg #3 assert");
  353. assert.equal(prng(), 0.9282578795792454, "Should be 'hello.'#1");
  354. return 'zzz';
  355. });
  356. assert.equal(result, 'zzz', "Should return value from callback.");
  357. assert.ok(original === Math.random, "Should not change Math.random.");
  358. // Global: false.
  359. // should be able to use new Math.seedrandom('hello.', {global: false})
  360. myrng = new Math.seedrandom('hello.', {global:false});
  361. assert.equal(typeof(myrng), 'function', "Should return a PRNG funciton.");
  362. assert.ok(original === Math.random, "Should not change Math.random.");
  363. assert.ok(original !== myrng, "PRNG should not be Math.random.");
  364. r = myrng();
  365. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  366. // options = {} when a method of Math.
  367. // should be able to use Math.seedrandom('hello.', {})
  368. result = Math.seedrandom('hello.');
  369. xprng = Math.random;
  370. assert.ok(original !== xprng, "Should change Math.random.");
  371. assert.equal(result, "hello.", "Should return short seed.");
  372. r = Math.random();
  373. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  374. r = Math.random();
  375. assert.equal(r, 0.3752569768646784, "Should be 'hello.'#2");
  376. Math.random = original;
  377. // options = {} when not a method of Math
  378. // should be able to use seedrandom('hello.', {})
  379. rng = seedrandom('hello.', {});
  380. assert.equal(typeof(rng), 'function', "Should return a function.");
  381. r = rng();
  382. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  383. assert.ok(original === Math.random, "Should not change Math.random.");
  384. assert.ok(original !== rng, "PRNG should not be Math.random.");
  385. });
  386. it('should support state api.', function() {
  387. // Verify that there is no state method
  388. var dummy = seedrandom('hello');
  389. var unexpected = -1;
  390. var expected = -1;
  391. try {
  392. unexpected = dummy.state();
  393. } catch(e) {
  394. expected = 1;
  395. }
  396. assert.equal(unexpected, -1);
  397. assert.equal(expected, 1);
  398. var count = 0;
  399. for (var x in dummy) {
  400. if (x == 'state') count += 1;
  401. }
  402. assert.equal(count, 0);
  403. // Verify that a state method can be added
  404. var saveable = seedrandom("secret-seed", {state: true});
  405. var ordinary = seedrandom("secret-seed");
  406. for (var j = 0; j < 1e2; ++j) {
  407. assert.equal(ordinary(), saveable());
  408. }
  409. var virgin = seedrandom("secret-seed");
  410. var saved = saveable.state();
  411. var replica = seedrandom("", {state: saved});
  412. for (var j = 0; j < 1e2; ++j) {
  413. var r = replica();
  414. assert.equal(r, saveable());
  415. assert.equal(r, ordinary());
  416. assert.ok(r != virgin());
  417. }
  418. });
  419. it('should support requirejs in node.', function() {
  420. var original = Math.random;
  421. var rsr = requirejs('../seedrandom');
  422. var rng = rsr('hello.');
  423. assert.equal(typeof(rng), 'function', "Should return a function.");
  424. var r = rng();
  425. assert.equal(r, 0.9282578795792454, "Should be 'hello.'#1");
  426. assert.ok(original === Math.random, "Should not change Math.random.");
  427. assert.ok(original !== rng, "PRNG should not be Math.random.");
  428. });
  429. // End of test.
  430. });
  431. }).call(this,"/test")
  432. },{"../seedrandom":3,"assert":"assert","requirejs":2}],"assert":[function(require,module,exports){
  433. // Use QUnit.assert to mimic node.assert.
  434. module.exports = QUnit.assert;
  435. },{}]},{},[4]);