PageRenderTime 69ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/php/math/mt_rand.js

http://github.com/kvz/phpjs
JavaScript | 20 lines | 13 code | 1 blank | 6 comment | 4 complexity | 37122e678f7fbd06e1d4b7adf4fd1f45 MD5 | raw file
  1. module.exports = function mt_rand (min, max) { // eslint-disable-line camelcase
  2. // discuss at: https://locutus.io/php/mt_rand/
  3. // original by: Onno Marsman (https://twitter.com/onnomarsman)
  4. // improved by: Brett Zamir (https://brett-zamir.me)
  5. // input by: Kongo
  6. // example 1: mt_rand(1, 1)
  7. // returns 1: 1
  8. const 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. }