PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/php/math/mt_rand.js

https://github.com/andriuspetrauskis/phpjs
JavaScript | 20 lines | 13 code | 1 blank | 6 comment | 4 complexity | 3c534f2d9f3b30081c843325d59c0bd8 MD5 | raw file
  1. module.exports = function mt_rand (min, max) { // eslint-disable-line camelcase
  2. // discuss at: http://locutus.io/php/mt_rand/
  3. // original by: Onno Marsman (https://twitter.com/onnomarsman)
  4. // improved by: Brett Zamir (http://brett-zamir.me)
  5. // input by: Kongo
  6. // example 1: mt_rand(1, 1)
  7. // returns 1: 1
  8. var argc = arguments.length
  9. if (argc === 0) {
  10. min = 0
  11. max = 2147483647
  12. } else if (argc === 1) {
  13. throw new Error('Warning: mt_rand() expects exactly 2 parameters, 1 given')
  14. } else {
  15. min = parseInt(min, 10)
  16. max = parseInt(max, 10)
  17. }
  18. return Math.floor(Math.random() * (max - min + 1)) + min
  19. }