Use PHP functions in JavaScript

JavaScript rand

Returns a random number

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
55
function rand (min, max) {
    // Returns a random number  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/rand    // +   original by: Leslie Hoare
    // +   bugfixed by: Onno Marsman
    // %          note 1: See the commented out code below for a version which will work with our experimental (though probably unnecessary) srand() function)
    // *     example 1: 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: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
        /*
    // See note above for an explanation of the following alternative code
    
    // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
    // -    depends on: srand    // %          note 1: This is a very possibly imperfect adaptation from the PHP source code
    var rand_seed, ctx, PHP_RAND_MAX=2147483647; // 0x7fffffff
 
    if (!this.php_js || this.php_js.rand_seed === undefined) {
        this.srand();    }
    rand_seed = this.php_js.rand_seed;
 
    var argc = arguments.length;
    if (argc === 1) {        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
 
    var do_rand = function (ctx) {
        return ((ctx * 1103515245 + 12345) % (PHP_RAND_MAX + 1));    };
 
    var php_rand = function (ctxArg) { // php_rand_r
        this.php_js.rand_seed = do_rand(ctxArg);
        return parseInt(this.php_js.rand_seed, 10);    };
 
    var number = php_rand(rand_seed);
 
    if (argc === 2) {        number = min + parseInt(parseFloat(parseFloat(max) - min + 1.0) * (number/(PHP_RAND_MAX + 1.0)), 10);
    }
    return number;
    */
}
external links: original PHP docs | raw js source

Examples

Running

1
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 rand goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
rtretretetert
Apr 12th Permalink

q  th

Gravatar
Kevin van Zonneveld
1 Feb '09 Permalink

q  @ taith: Sorry I don't have a lot of time to really dig in the sources. It would be great if your could explain what makes your implementation better than the one provided by Waldo?

Gravatar
taith
1 Feb '09 Permalink

q  [CODE="Javascript"]
function range(start, stop, step) {
if (!arguments.length) return [];
var min, max, step;
if (arguments.length == 1) {
min = 0;
max = arguments[0]-1;
step = 1;
}else{
min = arguments[0];
max = arguments[1];
step = arguments[2] || 1;
}
if (step < 0 && min >= max) {
step *= -1;
var tmp = min;
min = max;
max = tmp;
min += ((max-min) % step);
}
var a = [];
for (var i = min; i <= max; i += step) a[i] = i;
return a;
}
[/CODE]


Contribute a New function

More functions

In this category

abs
acos
acosh
asin
asinh
atan
atan2
atanh
base_convert
bindec
ceil
cos
cosh
decbin
dechex
decoct
deg2rad
exp
expm1
floor
fmod
getrandmax
hexdec
hypot
is_finite
is_infinite
is_nan
lcg_value
log
log10
log1p
max
min
mt_getrandmax
mt_rand
octdec
pi
pow
rad2deg
» rand
round
sin
sinh
sqrt
tan
tanh

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !