/website/source/php/math/mt_rand.html
HTML | 54 lines | 53 code | 1 blank | 0 comment | 0 complexity | 9a620c14bd39c07d923c8785b4f13498 MD5 | raw file
- ---
- warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand'
- examples:
- - 'mt_rand(1, 1)'
- estarget: es5
- returns:
- - '1'
- dependencies: []
- authors:
- original by:
- - 'Onno Marsman (https://twitter.com/onnomarsman)'
- improved by:
- - 'Brett Zamir (http://brett-zamir.me)'
- input by:
- - Kongo
- notes: []
- type: function
- layout: function
- title: PHP's mt_rand in JavaScript
- description: >-
- Here’s what our current JavaScript equivalent to <a
- href="http://php.net/manual/en/function.mt_rand.php">PHP's mt_rand</a> looks
- like.
- function: mt_rand
- category: math
- language: php
- permalink: php/math/mt_rand/
- alias:
- - /functions/php/mt_rand/
- - /functions/math/mt_rand/
- - /php/mt_rand/
- - /functions/mt_rand/
- ---
- {% codeblock lang:javascript %}module.exports = function mt_rand (min, max) { // eslint-disable-line camelcase
- // discuss at: http://locutus.io/php/mt_rand/
- // original by: Onno Marsman (https://twitter.com/onnomarsman)
- // improved by: Brett Zamir (http://brett-zamir.me)
- // input by: Kongo
- // example 1: mt_rand(1, 1)
- // returns 1: 1
- var argc = arguments.length
- if (argc === 0) {
- min = 0
- max = 2147483647
- } else if (argc === 1) {
- throw new Error('Warning: mt_rand() expects exactly 2 parameters, 1 given')
- } else {
- min = parseInt(min, 10)
- max = parseInt(max, 10)
- }
- return Math.floor(Math.random() * (max - min + 1)) + min
- }
- {% endcodeblock %}