prng4.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.math.random.prng4"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.math.random.prng4"] = true;
  8. dojo.provide("dojox.math.random.prng4");
  9. dojo.getObject("math.random.prng4", true, dojox);
  10. // Copyright (c) 2005 Tom Wu
  11. // All Rights Reserved.
  12. // See "LICENSE-BigInteger" for details.
  13. (function(){
  14. // prng4.js - uses Arcfour as a PRNG
  15. function Arcfour() {
  16. this.i = 0;
  17. this.j = 0;
  18. this.S = new Array(256);
  19. }
  20. dojo.extend(Arcfour, {
  21. init: function(key){
  22. // summary:
  23. // Initialize arcfour context
  24. // key: Array:
  25. // an array of ints, each from [0..255]
  26. var i, j, t, S = this.S, len = key.length;
  27. for(i = 0; i < 256; ++i){
  28. S[i] = i;
  29. }
  30. j = 0;
  31. for(i = 0; i < 256; ++i){
  32. j = (j + S[i] + key[i % len]) & 255;
  33. t = S[i];
  34. S[i] = S[j];
  35. S[j] = t;
  36. }
  37. this.i = 0;
  38. this.j = 0;
  39. },
  40. next: function(){
  41. var t, i, j, S = this.S;
  42. this.i = i = (this.i + 1) & 255;
  43. this.j = j = (this.j + S[i]) & 255;
  44. t = S[i];
  45. S[i] = S[j];
  46. S[j] = t;
  47. return S[(t + S[i]) & 255];
  48. }
  49. });
  50. dojox.math.random.prng4 = function(){
  51. return new Arcfour();
  52. };
  53. // Pool size must be a multiple of 4 and greater than 32.
  54. // An array of bytes the size of the pool will be passed to init()
  55. dojox.math.random.prng4.size = 256;
  56. })();
  57. }