From 5999362dc1abfd4a9f1e478bc6373af1b518f9e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20B=C3=B6hrnsen?= Date: Tue, 16 Dec 2014 14:16:47 +0100 Subject: update documentation of array_element --- lib/random.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/random.js') diff --git a/lib/random.js b/lib/random.js index a4bc1858..76cfbe90 100644 --- a/lib/random.js +++ b/lib/random.js @@ -37,7 +37,7 @@ var random = { }, - // takes an array and returns the array randomly sorted + // takes an array and returns a random element of the array array_element: function (array) { array = array || ["a", "b", "c"]; var r = faker.random.number({ max: array.length - 1 }); -- cgit v1.2.3 From 0b38c25af233ef0439f85f810f62c9469fd7834d Mon Sep 17 00:00:00 2001 From: James Drew Date: Mon, 22 Dec 2014 22:32:03 +0000 Subject: Added function to generate UUID --- lib/random.js | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib/random.js') diff --git a/lib/random.js b/lib/random.js index a4bc1858..83092653 100644 --- a/lib/random.js +++ b/lib/random.js @@ -51,6 +51,16 @@ var random = { var key = faker.random.array_element(array); return field === "key" ? key : object[key]; + }, + + uuid : function () { + var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; + var replacePlaceholders = function (placeholder) { + var random = Math.random()*16|0; + var value = placeholder == 'x' ? random : (random &0x3 | 0x8); + return value.toString(16); + }; + return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); } }; -- cgit v1.2.3 From cb82376c37716f765aee2a856567a02d1d1233ff Mon Sep 17 00:00:00 2001 From: Phil Greenberg Date: Wed, 10 Jun 2015 22:49:12 -0700 Subject: Fixing bug in random.number when max = 0 Also added tests for random.array_element --- lib/random.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/random.js') diff --git a/lib/random.js b/lib/random.js index 3205d8c4..0fc33451 100644 --- a/lib/random.js +++ b/lib/random.js @@ -26,7 +26,7 @@ var random = { // Make the range inclusive of the max value var max = options.max; - if (max > 0) { + if (max >= 0) { max += options.precision; } -- cgit v1.2.3 From 687e01b61440a638e7a28d88b33379994ac16cb2 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 10 Apr 2015 08:15:14 +0100 Subject: resolves #176 Add random boolean generator --- lib/random.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/random.js') diff --git a/lib/random.js b/lib/random.js index 0fc33451..cd60635a 100644 --- a/lib/random.js +++ b/lib/random.js @@ -61,6 +61,10 @@ var random = { return value.toString(16); }; return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); + }, + + boolean: function () { + return !!faker.random.number(1) } }; -- cgit v1.2.3 From eaf5c65cfae0a6636555884c18e6c955fae7887e Mon Sep 17 00:00:00 2001 From: Marak Date: Tue, 7 Jul 2015 16:22:50 -0700 Subject: [refactor] [dist] Allow for node to require individual locales ( to avoid the default behavior of requiring all locale data. #125 #167 --- lib/random.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/random.js') diff --git a/lib/random.js b/lib/random.js index cd60635a..41a06f28 100644 --- a/lib/random.js +++ b/lib/random.js @@ -1,5 +1,5 @@ var mersenne = require('../vendor/mersenne'); -var faker = require('../index'); +var faker = require('./index'); var random = { // returns a single random number based on a max number or range -- cgit v1.2.3 From e25e1ebc349c426c6b2598dc907eae497ef67160 Mon Sep 17 00:00:00 2001 From: Marak Date: Wed, 8 Jul 2015 14:34:39 -0700 Subject: [refactor] [major] Adds incremental browser builds. Switch to using prototype for internal API. Previous usage of `module.parent` is not acceptable. Locale information is now passed into Faker constructor. Closes #125 --- lib/random.js | 141 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 74 insertions(+), 67 deletions(-) (limited to 'lib/random.js') diff --git a/lib/random.js b/lib/random.js index 41a06f28..7d592849 100644 --- a/lib/random.js +++ b/lib/random.js @@ -1,71 +1,78 @@ var mersenne = require('../vendor/mersenne'); -var faker = require('./index'); - -var random = { - // returns a single random number based on a max number or range - number: function (options) { - - if (typeof options === "number") { - options = { - max: options - }; - } - - options = options || {}; - - if (typeof options.min === "undefined") { - options.min = 0; - } - - if (typeof options.max === "undefined") { - options.max = 1; - } - if (typeof options.precision === "undefined") { - options.precision = 1; - } - - // Make the range inclusive of the max value - var max = options.max; - if (max >= 0) { - max += options.precision; - } - - var randomNumber = options.precision * Math.floor( - mersenne.rand(max / options.precision, options.min / options.precision)); - - return randomNumber; - - }, - - // takes an array and returns a random element of the array - array_element: function (array) { - array = array || ["a", "b", "c"]; - var r = faker.random.number({ max: array.length - 1 }); - return array[r]; - }, - - // takes an object and returns the randomly key or value - object_element: function (object, field) { - object = object || {}; - var array = Object.keys(object); - var key = faker.random.array_element(array); - - return field === "key" ? key : object[key]; - }, - - uuid : function () { - var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; - var replacePlaceholders = function (placeholder) { - var random = Math.random()*16|0; - var value = placeholder == 'x' ? random : (random &0x3 | 0x8); - return value.toString(16); + +function Random (faker) { + + // returns a single random number based on a max number or range + this.number = function (options) { + + if (typeof options === "number") { + options = { + max: options }; - return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); - }, + } + + options = options || {}; + + if (typeof options.min === "undefined") { + options.min = 0; + } + + if (typeof options.max === "undefined") { + options.max = 1; + } + if (typeof options.precision === "undefined") { + options.precision = 1; + } + + // Make the range inclusive of the max value + var max = options.max; + if (max >= 0) { + max += options.precision; + } + + var randomNumber = options.precision * Math.floor( + mersenne.rand(max / options.precision, options.min / options.precision)); + + return randomNumber; + + } + + // takes an array and returns a random element of the array + this.array_element = function (array) { + array = array || ["a", "b", "c"]; + var r = faker.random.number({ max: array.length - 1 }); + return array[r]; + } + + // takes an object and returns the randomly key or value + this.object_element = function (object, field) { + object = object || {}; + var array = Object.keys(object); + var key = faker.random.array_element(array); + + return field === "key" ? key : object[key]; + } + + this.uuid = function () { + var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; + var replacePlaceholders = function (placeholder) { + var random = Math.random()*16|0; + var value = placeholder == 'x' ? random : (random &0x3 | 0x8); + return value.toString(16); + }; + return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); + } + + this.boolean =function () { + return !!faker.random.number(1) + } + + return this; + +} + +module['exports'] = Random; + - boolean: function () { - return !!faker.random.number(1) - } -}; -module.exports = random; +// module.exports = random; -- cgit v1.2.3 From 4c1b454b8e3c8d87f2569a2ce0d7730dc31ee821 Mon Sep 17 00:00:00 2001 From: Marak Date: Wed, 8 Jul 2015 23:56:22 -0700 Subject: [api] [refactor] Rename `array_element` and `object_element` to camelCase. Set default max random number to 99999. Added default arguments to some methods. --- lib/random.js | 70 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'lib/random.js') diff --git a/lib/random.js b/lib/random.js index 7d592849..2d37c551 100644 --- a/lib/random.js +++ b/lib/random.js @@ -5,50 +5,50 @@ function Random (faker) { // returns a single random number based on a max number or range this.number = function (options) { - if (typeof options === "number") { - options = { - max: options - }; - } - - options = options || {}; - - if (typeof options.min === "undefined") { - options.min = 0; - } - - if (typeof options.max === "undefined") { - options.max = 1; - } - if (typeof options.precision === "undefined") { - options.precision = 1; - } - - // Make the range inclusive of the max value - var max = options.max; - if (max >= 0) { - max += options.precision; - } - - var randomNumber = options.precision * Math.floor( - mersenne.rand(max / options.precision, options.min / options.precision)); - - return randomNumber; + if (typeof options === "number") { + options = { + max: options + }; + } + + options = options || {}; + + if (typeof options.min === "undefined") { + options.min = 0; + } + + if (typeof options.max === "undefined") { + options.max = 99999; + } + if (typeof options.precision === "undefined") { + options.precision = 1; + } + + // Make the range inclusive of the max value + var max = options.max; + if (max >= 0) { + max += options.precision; + } + + var randomNumber = options.precision * Math.floor( + mersenne.rand(max / options.precision, options.min / options.precision)); + + return randomNumber; } - + // takes an array and returns a random element of the array - this.array_element = function (array) { + this.arrayElement = function (array) { array = array || ["a", "b", "c"]; var r = faker.random.number({ max: array.length - 1 }); return array[r]; } // takes an object and returns the randomly key or value - this.object_element = function (object, field) { - object = object || {}; + this.objectElement = function (object, field) { + object = object || { "foo": "bar", "too": "car" }; var array = Object.keys(object); - var key = faker.random.array_element(array); + var key = faker.random.arrayElement(array); return field === "key" ? key : object[key]; } @@ -68,7 +68,7 @@ function Random (faker) { } return this; - + } module['exports'] = Random; -- cgit v1.2.3 From 40c13f210afd9a2cd5bf5f60fba7169c6e35ea44 Mon Sep 17 00:00:00 2001 From: Brandon Dail Date: Wed, 15 Jul 2015 13:31:51 -0500 Subject: Implemented faker.seed method for randomization seeding --- lib/random.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'lib/random.js') diff --git a/lib/random.js b/lib/random.js index 2d37c551..fc7e54d9 100644 --- a/lib/random.js +++ b/lib/random.js @@ -1,7 +1,15 @@ var mersenne = require('../vendor/mersenne'); -function Random (faker) { - +function Random (faker, seed) { + // Use a user provided seed if it exists + if (seed) { + if (Array.isArray(seed) && seed.length) { + mersenne.seed_array(seed); + } + else { + mersenne.seed(seed); + } + } // returns a single random number based on a max number or range this.number = function (options) { @@ -28,7 +36,7 @@ function Random (faker) { var max = options.max; if (max >= 0) { max += options.precision; - } + } var randomNumber = options.precision * Math.floor( mersenne.rand(max / options.precision, options.min / options.precision)); -- cgit v1.2.3