UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

122 lines (99 loc) 2.84 kB
<!-- Generated by Rakefile:build --> <strong> Nathan </strong> on 2011-05-09 18:14:40 <br /> Sorry, the correct code would be <pre><code>function rand (min, max) { var argc = arguments.length; if (argc === 0) { min = 0; max = 2147483647; } else if (argc === 1) { throw new Error('Warning: rand() expects exactly 2 parameters, 1 given'); } if (min &gt; max) { var mint = min; min = max; max = mint; } if (min &gt;= 0) return Math.floor(Math.random() * (max - min + 1)) + min; else { var result = Math.random() * (max + Math.abs(min) + 1) - Math.abs(min); if (Math.round(min) == min || Math.round(max) == max) return Math.floor(result); else return result; } }</code></pre> Only use new code if min is negative. <hr /> <strong> Nathan </strong> on 2011-05-09 18:05:28 <br /> The code given here does not support negatives or floats, nor does it support an input of max,min instead of min,max. All of which the PHP version does support. I made some minor tweaks to get proper support for those &quot;features&quot;, not that I am not a JS pro and there is probably a better way to go about this. <pre><code>function rand (min, max) { var argc = arguments.length; if (argc === 0) { min = 0; max = 2147483647; } else if (argc === 1) { throw new Error('Warning: rand() expects exactly 2 parameters, 1 given'); } if (min &gt; max) { var mint = min; min = max; max = mint; } var result = Math.random() * (max + Math.abs(min) + 1) - Math.abs(min); if (Math.round(min) == min || Math.round(max) == max) return Math.floor(result); else return result; }</code></pre> <hr /> <strong> test </strong> on 2010-11-04 19:46:43 <br /> <pre><code> your_stuff('here'); </code></pre> <hr /> <strong> rtretretetert </strong> on 2010-04-12 10:26:52 <br /> th <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-02-01 23:14:36 <br /> @ taith: Sorry I don't have a lot of time to really dig in the sources. It would be great if your could explain what makes your implementation better than the one provided by Waldo? <hr /> <strong> <a href="divinedesigns.ca" rel="nofollow">taith</a> </strong> on 2009-02-01 14:01:09 <br /> <pre><code> function range(start, stop, step) { if (!arguments.length) return []; var min, max, step; if (arguments.length == 1) { min = 0; max = arguments[0]-1; step = 1; }else{ min = arguments[0]; max = arguments[1]; step = arguments[2] || 1; } if (step &amp;lt; 0 &amp;amp;&amp;amp; min &amp;gt;= max) { step *= -1; var tmp = min; min = max; max = tmp; min += ((max-min) % step); } var a = []; for (var i = min; i &amp;lt;= max; i += step) a[i] = i; return a; } </code></pre> <hr />