Advertisement
tolikpunkoff

random.js

Jul 30th, 2021
2,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // random.js
  2.  
  3. // call this from the command line with:
  4. // C:\> cscript /nologo random.js
  5.  
  6. // or from PowerShell
  7. // PS C:\> $myrandom = & cscript /nologo "c:\batch\random.js"
  8. // will create an array of 10 random numbers which you can then treat like any array variable:
  9. // PS C:\> $myrandom[4]
  10.  
  11. // Calling without a seed, the current time will be used as a seed
  12. var srandom=Alea();
  13.  
  14. // Calling with a seed will return the same value for the same seed
  15. //var seed=1234
  16. //var srandom=Alea(seed);
  17.  
  18. var i=0
  19.  
  20.   // Return 10 random numbers
  21. while ( i < 10 ) {
  22.   // Return a number between 1 and 500 million
  23.   WScript.echo(Math.floor((srandom()*500000000)+1) );
  24.   i++;
  25. }
  26.  
  27. function Mash() {
  28.   var n = 0xefc8249d;
  29.  
  30.   var mash = function(data) {
  31.     data = data.toString();
  32.     for (var i = 0; i < data.length; i++) {
  33.       n += data.charCodeAt(i);
  34.       var h = 0.02519603282416938 * n;
  35.       n = h >>> 0;
  36.       h -= n;
  37.       h *= n;
  38.       n = h >>> 0;
  39.       h -= n;
  40.       n += h * 0x100000000; // 2^32
  41.     }
  42.     return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
  43.   };
  44.  
  45.   mash.version = 'Mash 0.9';
  46.   return mash;
  47. }
  48.  
  49. function Alea() {
  50.   return (function(args) {
  51.     // Johannes Baagoe <baagoe@baagoe.com>, 2010
  52.     var s0 = 0;
  53.     var s1 = 0;
  54.     var s2 = 0;
  55.     var c = 1;
  56.  
  57.     if (args.length == 0) {
  58.       args = [+new Date];
  59.     }
  60.     var mash = Mash();
  61.     s0 = mash(' ');
  62.     s1 = mash(' ');
  63.     s2 = mash(' ');
  64.  
  65.     for (var i = 0; i < args.length; i++) {
  66.       s0 -= mash(args[i]);
  67.       if (s0 < 0) {
  68.         s0 += 1;
  69.       }
  70.       s1 -= mash(args[i]);
  71.       if (s1 < 0) {
  72.         s1 += 1;
  73.       }
  74.       s2 -= mash(args[i]);
  75.       if (s2 < 0) {
  76.         s2 += 1;
  77.       }
  78.     }
  79.     mash = null;
  80.  
  81.     var random = function() {
  82.       var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
  83.       s0 = s1;
  84.       s1 = s2;
  85.       return s2 = t - (c = t | 0);
  86.     };
  87.     random.uint32 = function() {
  88.       return random() * 0x100000000; // 2^32
  89.     };
  90.     random.fract53 = function() {
  91.       return random() +
  92.         (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
  93.     };
  94.     random.version = 'Alea 0.9';
  95.     random.args = args;
  96.     return random;
  97.  
  98.   } (Array.prototype.slice.call(arguments)));
  99. };
  100.  
  101. /* licensed according to the MIT - Expat license:
  102.  
  103. Copyright (C) 2010 by Johannes Baagoe <baagoe@baagoe.org>
  104.  
  105. Permission is hereby granted, free of charge, to any person obtaining a copy
  106. of this software and associated documentation files (the "Software"), to deal
  107. in the Software without restriction, including without limitation the rights
  108. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  109. copies of the Software, and to permit persons to whom the Software is
  110. furnished to do so, subject to the following conditions:
  111.  
  112. The above copyright notice and this permission notice shall be included in
  113. all copies or substantial portions of the Software.
  114.  
  115. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  116. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  117. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  118. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  119. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  120. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  121. THE SOFTWARE. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement