PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

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