JavaScript mt_rand
Returns a random number from Mersenne Twister
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 | function mt_rand (min, max) { // Returns a random number from Mersenne Twister // // version: 1008.1718 // discuss at: http://phpjs.org/functions/mt_rand // + original by: Onno Marsman // * 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'); } return Math.floor(Math.random() * (max - min + 1)) + min; } |
Examples
Running
1 | mt_rand(1, 1); |
Should return
1 | 1 |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have mt_rand goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>