PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/website/source/php/math/mt_rand.html

https://github.com/andriuspetrauskis/phpjs
HTML | 54 lines | 53 code | 1 blank | 0 comment | 0 complexity | 9a620c14bd39c07d923c8785b4f13498 MD5 | raw file
  1. ---
  2. warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand'
  3. examples:
  4. - 'mt_rand(1, 1)'
  5. estarget: es5
  6. returns:
  7. - '1'
  8. dependencies: []
  9. authors:
  10. original by:
  11. - 'Onno Marsman (https://twitter.com/onnomarsman)'
  12. improved by:
  13. - 'Brett Zamir (http://brett-zamir.me)'
  14. input by:
  15. - Kongo
  16. notes: []
  17. type: function
  18. layout: function
  19. title: PHP's mt_rand in JavaScript
  20. description: >-
  21. Heres what our current JavaScript equivalent to <a
  22. href="http://php.net/manual/en/function.mt_rand.php">PHP's mt_rand</a> looks
  23. like.
  24. function: mt_rand
  25. category: math
  26. language: php
  27. permalink: php/math/mt_rand/
  28. alias:
  29. - /functions/php/mt_rand/
  30. - /functions/math/mt_rand/
  31. - /php/mt_rand/
  32. - /functions/mt_rand/
  33. ---
  34. {% codeblock lang:javascript %}module.exports = function mt_rand (min, max) { // eslint-disable-line camelcase
  35. // discuss at: http://locutus.io/php/mt_rand/
  36. // original by: Onno Marsman (https://twitter.com/onnomarsman)
  37. // improved by: Brett Zamir (http://brett-zamir.me)
  38. // input by: Kongo
  39. // example 1: mt_rand(1, 1)
  40. // returns 1: 1
  41. var argc = arguments.length
  42. if (argc === 0) {
  43. min = 0
  44. max = 2147483647
  45. } else if (argc === 1) {
  46. throw new Error('Warning: mt_rand() expects exactly 2 parameters, 1 given')
  47. } else {
  48. min = parseInt(min, 10)
  49. max = parseInt(max, 10)
  50. }
  51. return Math.floor(Math.random() * (max - min + 1)) + min
  52. }
  53. {% endcodeblock %}