Skip to content

Commit

Permalink
Fixed issue #06149: EM shows wrong result for number generated by ran…
Browse files Browse the repository at this point in the history
…d() function

Dev was using the JavaScript Math.random() function instead of the phpjs.org rand() function
  • Loading branch information
TMSWhite committed May 31, 2012
1 parent 3a77c80 commit 4ba0df1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion application/helpers/expressions/em_core_helper.php
Expand Up @@ -196,7 +196,7 @@ function __construct()
'quoted_printable_decode' => array('quoted_printable_decode', 'quoted_printable_decode', $this->gT('Convert a quoted-printable string to an 8 bit string'), 'string quoted_printable_decode(string)', 'http://www.php.net/manual/en/function.quoted-printable-decode.php', 1),
'quoted_printable_encode' => array('quoted_printable_encode', 'quoted_printable_encode', $this->gT('Convert a 8 bit string to a quoted-printable string'), 'string quoted_printable_encode(string)', 'http://www.php.net/manual/en/function.quoted-printable-encode.php', 1),
'quotemeta' => array('quotemeta', 'quotemeta', $this->gT('Quote meta characters'), 'string quotemeta(string)', 'http://www.php.net/manual/en/function.quotemeta.php', 1),
'rand' => array('rand', 'Math.random', $this->gT('Generate a random integer'), 'int rand() OR int rand(min, max)', 'http://www.php.net/manual/en/function.rand.php', 0,2),
'rand' => array('rand', 'rand', $this->gT('Generate a random integer'), 'int rand() OR int rand(min, max)', 'http://www.php.net/manual/en/function.rand.php', 0,2),
'regexMatch' => array('exprmgr_regexMatch', 'LEMregexMatch', $this->gT('Compare a string to a regular expression pattern'), 'bool regexMatch(pattern,input)', '', 2),
'round' => array('round', 'round', $this->gT('Rounds a number to an optional precision'), 'number round(val [, precision])', 'http://www.php.net/manual/en/function.round.php', 1,2),
'rtrim' => array('rtrim', 'rtrim', $this->gT('Strip whitespace (or other characters) from the end of a string'), 'string rtrim(string [, charlist])', 'http://www.php.net/manual/en/function.rtrim.php', 1,2),
Expand Down
17 changes: 17 additions & 0 deletions scripts/expressions/em_javascript.js
Expand Up @@ -2483,6 +2483,23 @@ function mktime () {
return (d.getTime() / 1e3 >> 0) - (d.getTime() < 0);
}

function rand (min, max) {
// http://kevin.vanzonneveld.net
// + 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;
}

function time () {
// Return current UNIX timestamp
//
Expand Down

0 comments on commit 4ba0df1

Please sign in to comment.