diff --git a/README.md b/README.md index ca8fec9..aae1d97 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ require "faker" Faker::Name.name ``` +### Faker.seed + +If you wish to seed the random data, you can call `Faker.seed number` and then all subsequent calls will be deterministic. + ### Faker::Address ```crystal diff --git a/spec/address_spec.cr b/spec/address_spec.cr new file mode 100644 index 0000000..7db4de6 --- /dev/null +++ b/spec/address_spec.cr @@ -0,0 +1,24 @@ +require "./spec_helper" + +describe Faker::Address do + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Address.city.should eq "North Mercedesmouth" + Faker::Address.street_name.should eq "Thalia Tunnel" + Faker::Address.street_address.should eq "33421 Kunde Mills" + Faker::Address.secondary_address.should eq "Apt. 475" + Faker::Address.building_number.should eq "4103" + Faker::Address.zip_code.should eq "32555" + Faker::Address.postcode.should eq "GX44 3IC" + Faker::Address.time_zone.should eq "Asia/Rangoon" + Faker::Address.street_suffix.should eq "Springs" + Faker::Address.city_suffix.should eq "town" + Faker::Address.city_prefix.should eq "North" + Faker::Address.state.should eq "Missouri" + Faker::Address.state_abbr.should eq "TN" + Faker::Address.country.should eq "Svalbard & Jan Mayen Islands" + Faker::Address.country_code.should eq "IS" + Faker::Address.latitude.should eq "15.393888556377334" + Faker::Address.longitude.should eq "-77.86301819889268" + end +end diff --git a/spec/business_spec.cr b/spec/business_spec.cr new file mode 100644 index 0000000..a3954bb --- /dev/null +++ b/spec/business_spec.cr @@ -0,0 +1,10 @@ +require "./spec_helper" + +describe Faker::Business do + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Business.credit_card_number.should eq "1234-2121-1221-1211" + Faker::Business.credit_card_expiry_date.should eq Time.new(2013, 9, 12) + Faker::Business.credit_card_type.should eq "diners_club" + end +end diff --git a/spec/commerce_spec.cr b/spec/commerce_spec.cr index 3d85416..c23cac0 100644 --- a/spec/commerce_spec.cr +++ b/spec/commerce_spec.cr @@ -52,4 +52,15 @@ describe Faker::Commerce do it "price_is_float" do Faker::Commerce.price.is_a?(Float).should be_true end + + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Commerce.color.should eq "salmon" + Faker::Commerce.department.should eq "Home, Movies & Computers" + Faker::Commerce.department(5).should eq "Toys & Home" + Faker::Commerce.department(2, true).should eq "Electronics & Tools" + Faker::Commerce.product_name.should eq "Heavy Duty Copper Lamp" + Faker::Commerce.price.should eq 47.25 + Faker::Commerce.material.should eq "Rubber" + end end diff --git a/spec/company_spec.cr b/spec/company_spec.cr index 677299f..62ca2f6 100644 --- a/spec/company_spec.cr +++ b/spec/company_spec.cr @@ -5,4 +5,11 @@ describe Faker::Company do it "logo" do Faker::Company.logo.match(%r{https://pigment.github.io/fake-logos/logos/medium/color/\d+\.png}).should_not eq nil end + + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Company.name.should eq "Larson-Altenwerth" + Faker::Company.suffix.should eq "Group" + Faker::Company.logo.should eq "https://pigment.github.io/fake-logos/logos/medium/color/12.png" + end end diff --git a/spec/hacker_spec.cr b/spec/hacker_spec.cr index e267934..0c4d776 100644 --- a/spec/hacker_spec.cr +++ b/spec/hacker_spec.cr @@ -29,4 +29,14 @@ describe Faker::Hacker do it "ingverb" do Faker::Hacker.ingverb.match(/\w+/).should_not eq nil end + + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Hacker.say_something_smart.should eq "You can't override the sensor without overriding the primary JBOD transmitter!" + Faker::Hacker.abbreviation.should eq "SCSI" + Faker::Hacker.adjective.should eq "primary" + Faker::Hacker.noun.should eq "pixel" + Faker::Hacker.verb.should eq "transmit" + Faker::Hacker.ingverb.should eq "calculating" + end end diff --git a/spec/internet_spec.cr b/spec/internet_spec.cr index 62fd83a..0f9c65c 100644 --- a/spec/internet_spec.cr +++ b/spec/internet_spec.cr @@ -166,4 +166,35 @@ describe Faker::Internet do it "device_token" do # Faker::Internet.device_token.size.should eq 64 end + + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Internet.email.should eq "internet_faker@kirlinblock.biz" + Faker::Internet.email("Nancy").should eq "nancy@cremin.io" + Faker::Internet.free_email.should eq "faker.internet@gmail.com" + Faker::Internet.free_email("Nancy").should eq "nancy@hotmail.com" + Faker::Internet.safe_email.should eq "faker_internet@example.net" + Faker::Internet.safe_email("Nancy").should eq "nancy@example.net" + Faker::Internet.user_name.should eq "faker.internet" + Faker::Internet.user_name("Nancy").should eq "nancy" + Faker::Internet.user_name("Nancy Johnson", [".", "_", "-"] of ::String).should eq "johnson.nancy" + Faker::Internet.password.should eq "Tb34Ap05U" + Faker::Internet.password(8).should eq "BhI59sBo9" + Faker::Internet.password(10, 20).should eq "9r3b4iL8HeY" + Faker::Internet.password(10, 20, true).should eq "91JpPrF867Z" + Faker::Internet.password(10, 20, true, true).should eq "Pl5q3eOqZsK" + Faker::Internet.domain_name.should eq "ullrichklocko.biz" + Faker::Internet.domain_word.should eq "lockman" + Faker::Internet.domain_suffix.should eq "net" + Faker::Internet.ip_v4_address.should eq "6.70.63.244" + Faker::Internet.ip_v6_address.should eq "8a31:c21c:fb41:eea8:9df:aeab:9f78:5a49" + Faker::Internet.mac_address.should eq "00:4b:14:3d:24:55" + Faker::Internet.mac_address("55:44:33").should eq "55:44:33:36:0e:5d" + Faker::Internet.url.should eq "http://koelpin.biz/internet_faker" + Faker::Internet.url("example.com").should eq "http://example.com/faker_internet" + Faker::Internet.url("example.com", "/foobar.html").should eq "http://example.com/foobar.html" + Faker::Internet.slug.should eq "voluptatem.dicta" + Faker::Internet.slug("foo bar").should eq "foo.bar" + Faker::Internet.slug("foo bar", "-").should eq "foo-bar" + end end diff --git a/spec/lorem_spec.cr b/spec/lorem_spec.cr new file mode 100644 index 0000000..ad3fe58 --- /dev/null +++ b/spec/lorem_spec.cr @@ -0,0 +1,33 @@ +require "./spec_helper" + +describe Faker::Lorem do + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Lorem.word.should eq "voluptatem" + Faker::Lorem.words.should eq ["quos", "recusandae", "sed"] + Faker::Lorem.words(4).should eq ["iusto", "quia", "placeat", "quia"] + Faker::Lorem.words(4, true).should eq ["vis", "ambulo", "vesica", "vivo"] + Faker::Lorem.characters.should eq "xk7jz4c7a0w5ceh9opz68kq3xzdt5d2rtu29ky6jlb1ltxk3xvqkp87o2c7bs070wggzuz78an6t4cg8fejvhskgfe6ry104yc7kam8p91oaf6bqb61tp84q4958om8glep3wjrl9no32fj1eoklww8bzhvzmdkyp1tn51zxpzm8eynd7mbix1rimwrpufnut2a58d76mwrn4epjao7pedef2roosl2simxsvi4lejwzrdojegew2csobpw2lym" + Faker::Lorem.characters(10).should eq "vp5j8u3r1l" + Faker::Lorem.sentence.should eq "Itaque quod aliquam veritatis nemo dolorum sequi." + Faker::Lorem.sentence(3).should eq "Accusantium dolores suscipit praesentium." + Faker::Lorem.sentence(3, true).should eq "Spiculum triumphus necessitatibus appono angulus aut cariosus." + Faker::Lorem.sentence(3, false, 4).should eq "Atque sed perferendis minus." + Faker::Lorem.sentence(3, true, 4).should eq "Cohors traho tamdiu alveus itaque." + Faker::Lorem.sentences.should eq ["Sed ab quia.", + "Consequatur doloremque dolores ratione voluptas esse aut vero.", + "Qui dicta est odit nihil exercitationem nesciunt."] + Faker::Lorem.sentences(1).should eq ["Reiciendis doloremque vero sequi et quia."] + Faker::Lorem.sentences(1, true).should eq ["Concido quisquam cerno vulpes maxime mollitia."] + Faker::Lorem.paragraph.should eq "Maxime magnam vel velit id. Sed cupiditate blanditiis. Cupiditate sit aliquam vero." + Faker::Lorem.paragraph(2).should eq "Nemo laboriosam quod accusamus ab modi excepturi. Eveniet rerum quasi quia culpa. Vero quis aut. A assumenda tenetur." + Faker::Lorem.paragraph(2, true).should eq "Pecunia sopor ars vero subiungo quis urbanus. Demens minus delinquo." + Faker::Lorem.paragraph(2, false, 4).should eq "Mollitia delectus qui et ut quasi. Eligendi aut facilis praesentium ducimus qui. Itaque molestiae laudantium qui omnis veritatis soluta. Quia reiciendis dignissimos saepe architecto. Eum excepturi dolores iusto quisquam." + Faker::Lorem.paragraph(2, true, 4).should eq "Depraedor vitae spectaculum cupio acervus conatus adipisci. Pecus vulgivagus terra aliqua arbitro. Suscipio socius cupiditas arguo cetera colloco trans solitudo. Adimpleo stillicidium eaque ocer placeat adstringo ustulo balbus. Versus viduo videlicet tenus absconditus." + Faker::Lorem.paragraphs.should eq ["Rerum odit ducimus ipsum at sint. Consequatur sed non dolorem. Incidunt quae libero. Enim quam molestiae necessitatibus.", + "Inventore eos dolores asperiores corrupti cupiditate. Illum at molestiae cumque eaque aut. Blanditiis et laudantium neque. Consequatur labore officiis pariatur possimus facilis.", + "Esse fuga cum quisquam tenetur impedit rem repellat. Quia deleniti nulla. Excepturi perferendis dolor accusamus."] + Faker::Lorem.paragraphs(1).should eq ["Rerum unde voluptas sint autem. Fugit tenetur sequi totam esse. Explicabo omnis consequatur inventore autem. Et et praesentium neque quo natus."] + Faker::Lorem.paragraphs(1, true).should eq ["Conitor reiciendis minima viridis temporibus spiritus. Tabula crustulum error titulus et comptus. Coruscus turpis amplitudo combibo alias deludo triginta."] + end +end diff --git a/spec/name_spec.cr b/spec/name_spec.cr new file mode 100644 index 0000000..d7198cf --- /dev/null +++ b/spec/name_spec.cr @@ -0,0 +1,13 @@ +require "./spec_helper" + +describe Faker::Name do + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Name.name.should eq "Bernie Altenwerth" + Faker::Name.first_name.should eq "Ron" + Faker::Name.last_name.should eq "Block" + Faker::Name.prefix.should eq "Mrs." + Faker::Name.suffix.should eq "Sr." + Faker::Name.title.should eq "Product Markets Specialist" + end +end diff --git a/spec/number_spec.cr b/spec/number_spec.cr index 69c2fd3..331f641 100644 --- a/spec/number_spec.cr +++ b/spec/number_spec.cr @@ -64,4 +64,16 @@ describe Faker::Number do Faker::Number.hexadecimal(4).match(/[0-9a-f]{4}/).should_not eq nil Faker::Number.hexadecimal(7).match(/[0-9a-f]{7}/).should_not eq nil end + + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Number.number(10).should eq "6961710422" + Faker::Number.decimal(2).should eq "82.34" + Faker::Number.decimal(2, 3).should eq "21.217" + Faker::Number.hexadecimal(3).should eq "046" + Faker::Number.between(1, 10).should eq 2 + Faker::Number.positive.should eq 3995.8054520295627 + Faker::Number.negative.should eq -748.5527873256278 + Faker::Number.digit.should eq "2" + end end diff --git a/spec/phone_number_spec.cr b/spec/phone_number_spec.cr new file mode 100644 index 0000000..d5548ca --- /dev/null +++ b/spec/phone_number_spec.cr @@ -0,0 +1,8 @@ +require "./spec_helper" + +describe Faker::PhoneNumber do + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::PhoneNumber.phone_number.should eq "361-710-4228 x23421" + end +end diff --git a/spec/team_spec.cr b/spec/team_spec.cr index 14abf5e..bdc1aae 100644 --- a/spec/team_spec.cr +++ b/spec/team_spec.cr @@ -16,4 +16,11 @@ describe Faker::Team do it "sport" do # Faker::Team.sport.match(/(\w+){1}/).should_not eq nil end + + it "should return deterministic results when seeded" do + Faker.seed 123456 + Faker::Team.creature.should eq "buffalo" + Faker::Team.name.should eq "Washington sorcerors" + Faker::Team.state.should eq "Delaware" + end end diff --git a/src/faker.cr b/src/faker.cr index a4e575c..de1ab86 100644 --- a/src/faker.cr +++ b/src/faker.cr @@ -2,13 +2,19 @@ require "./data.cr" require "./faker/*" module Faker + class_getter rng : Random = Random.new + + def self.seed(number) + @@rng = Random.new number + end + def self.numerify(number_string) number_string = number_string.as String - number_string.sub(/#/) { (rand(9) + 1).to_s }.gsub(/#/) { rand(10).to_s } + number_string.sub(/#/) { (@@rng.rand(9) + 1).to_s }.gsub(/#/) { @@rng.rand(10).to_s } end def self.letterify(letter_string) - letter_string.gsub(/\?/) { ("A".."Z").to_a.sample } + letter_string.gsub(/\?/) { ("A".."Z").to_a.sample(@@rng) } end def self.bothify(string) @@ -17,21 +23,21 @@ module Faker def self.regexify(re) re = re.source if re.is_a?(Regex) - re.gsub(/^\/?\^?/, "").gsub(/\$?\/?$/, "") # Ditch the anchors - .gsub(/\{(\d+)\}/) { "{#{$1},#{$1}}" }.gsub(/\?/, "{0,1}") # All {2} become {2,2} and ? become {0,1} - .gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |match| $1 * (Range.new($2.to_i, $3.to_i)).to_a.sample } # [12]{1,2} becomes [12] or [12][12] - .gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # (12|34){1,2} becomes (12|34) or (12|34)(12|34) - .gsub(/(\\?.)\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d - .gsub(/\((.*?)\)/) { |match| match.gsub(/[\(\)]/, "").split("|").sample } # (this|that) becomes "this" or "that" - .gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w\-\w)/) { |range| ((0...range.size).map { |i| range[i].to_s }).join("").split("-").to_a.sample } } # All A-Z inside of [] become C (or X, or whatever) - .gsub(/\[([^\]]+)\]/) { |match| $1.split("").sample } # All [ABC] become B (or A or C) - .gsub("\\d") { |match| (0..9).to_a.sample } - .gsub("\\w") { |match| (("A".."Z").to_a + (0..9).to_a).sample } + re.gsub(/^\/?\^?/, "").gsub(/\$?\/?$/, "") # Ditch the anchors + .gsub(/\{(\d+)\}/) { "{#{$1},#{$1}}" }.gsub(/\?/, "{0,1}") # All {2} become {2,2} and ? become {0,1} + .gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |match| $1 * (Range.new($2.to_i, $3.to_i)).to_a.sample(@@rng) } # [12]{1,2} becomes [12] or [12][12] + .gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample(@@rng) } # (12|34){1,2} becomes (12|34) or (12|34)(12|34) + .gsub(/(\\?.)\{(\d+),(\d+)\}/) { |match| $1 * ($2.to_i..$3.to_i).to_a.sample(@@rng) } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d + .gsub(/\((.*?)\)/) { |match| match.gsub(/[\(\)]/, "").split("|").sample(@@rng) } # (this|that) becomes "this" or "that" + .gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w\-\w)/) { |range| ((0...range.size).map { |i| range[i].to_s }).join("").split("-").to_a.sample(@@rng) } } # All A-Z inside of [] become C (or X, or whatever) + .gsub(/\[([^\]]+)\]/) { |match| $1.split("").sample(@@rng) } # All [ABC] become B (or A or C) + .gsub("\\d") { |match| (0..9).to_a.sample(@@rng) } + .gsub("\\w") { |match| (("A".."Z").to_a + (0..9).to_a).sample(@@rng) } end def self.fetch(data) data = data.as Array - fetched = data.sample.as String + fetched = data.sample(@@rng).as String if fetched.match(/^\//) && fetched.match(/\/$/) # A regex fetched = Faker.regexify(fetched) end @@ -74,6 +80,6 @@ module Faker # Generates a random value between the interval def self.rand_in_range(from, to) from, to = to, from if to < from - Random.new.rand(from..to) + @@rng.rand(from..to) end end diff --git a/src/faker/address.cr b/src/faker/address.cr index 82a9e99..9a9720d 100644 --- a/src/faker/address.cr +++ b/src/faker/address.cr @@ -1,7 +1,7 @@ module Faker class Address def self.zip_code - Faker.numerify(["#####", "#####-####"].sample) + Faker.numerify(["#####", "#####-####"].sample(Faker.rng)) end {% for data_type in %w(state state_abbr city_suffix city_prefix country street_suffix country_code) %} @@ -18,7 +18,7 @@ module Faker [ ->{ [Name.last_name, street_suffix].join(" ") }, ->{ [Name.first_name, street_suffix].join(" ") }, - ].sample.call + ].sample(Faker.rng).call end def self.street_address @@ -29,7 +29,7 @@ module Faker ->{ "##### %s" % street_name }, ->{ "##### %s Apt. ###" % street_name }, ->{ "##### %s Suite ###" % street_name }, - ].sample.call) + ].sample(Faker.rng).call) end def self.secondary_address @@ -44,15 +44,15 @@ module Faker Faker.bothify([ ->{ "??# #??" }, ->{ "??## #??" }, - ].sample.call) + ].sample(Faker.rng).call) end def self.latitude - ((rand * 180) - 90).to_s + ((Faker.rng.rand * 180) - 90).to_s end def self.longitude - ((rand * 360) - 180).to_s + ((Faker.rng.rand * 360) - 180).to_s end def self.time_zone diff --git a/src/faker/commerce.cr b/src/faker/commerce.cr index 8e68ed2..92426a3 100644 --- a/src/faker/commerce.cr +++ b/src/faker/commerce.cr @@ -6,7 +6,7 @@ module Faker def self.department(max = 3, fixed_amount = false) num = max if fixed_amount - num ||= 1 + rand(max) + num ||= 1 + Faker.rng.rand(max) categories = categories(num) @@ -28,7 +28,7 @@ module Faker end def self.price(range = 0.0..100.0) - (rand(range) * 100).floor/100.0 + (Faker.rng.rand(range) * 100).floor/100.0 end private def self.categories(num) diff --git a/src/faker/company.cr b/src/faker/company.cr index 39603ba..22e4874 100644 --- a/src/faker/company.cr +++ b/src/faker/company.cr @@ -19,7 +19,7 @@ module Faker end def self.logo - rand_num = Random.rand(13) + 1 + rand_num = Faker.rng.rand(13) + 1 "https://pigment.github.io/fake-logos/logos/medium/color/#{rand_num}.png" end end diff --git a/src/faker/hacker.cr b/src/faker/hacker.cr index 6db0cac..7f6cfb9 100644 --- a/src/faker/hacker.cr +++ b/src/faker/hacker.cr @@ -2,7 +2,7 @@ module Faker class Hacker def self.say_something_smart - phrases.sample + phrases.sample(Faker.rng) end {% for data_type in %w(abbreviation adjective noun verb ingverb) %} @@ -13,13 +13,13 @@ module Faker def self.phrases ["If we #{verb} the #{noun}, we can get to the #{abbreviation} #{noun} through the #{adjective} #{abbreviation} #{noun}!", - "We need to #{verb} the #{adjective} #{abbreviation} #{noun}!", - "Try to #{verb} the #{abbreviation} #{noun}, maybe it will #{verb} the #{adjective} #{noun}!", - "You can't #{verb} the #{noun} without #{ingverb} the #{adjective} #{abbreviation} #{noun}!", - "Use the #{adjective} #{abbreviation} #{noun}, then you can #{verb} the #{adjective} #{noun}!", - "The #{abbreviation} #{noun} is down, #{verb} the #{adjective} #{noun} so we can #{verb} the #{abbreviation} #{noun}!", - "#{ingverb} the #{noun} won't do anything, we need to #{verb} the #{adjective} #{abbreviation} #{noun}!", - "I'll #{verb} the #{adjective} #{abbreviation} #{noun}, that should #{noun} the #{abbreviation} #{noun}!", + "We need to #{verb} the #{adjective} #{abbreviation} #{noun}!", + "Try to #{verb} the #{abbreviation} #{noun}, maybe it will #{verb} the #{adjective} #{noun}!", + "You can't #{verb} the #{noun} without #{ingverb} the #{adjective} #{abbreviation} #{noun}!", + "Use the #{adjective} #{abbreviation} #{noun}, then you can #{verb} the #{adjective} #{noun}!", + "The #{abbreviation} #{noun} is down, #{verb} the #{adjective} #{noun} so we can #{verb} the #{abbreviation} #{noun}!", + "#{ingverb} the #{noun} won't do anything, we need to #{verb} the #{adjective} #{abbreviation} #{noun}!", + "I'll #{verb} the #{adjective} #{abbreviation} #{noun}, that should #{noun} the #{abbreviation} #{noun}!", ] end end diff --git a/src/faker/internet.cr b/src/faker/internet.cr index 3025f93..a4f21d3 100644 --- a/src/faker/internet.cr +++ b/src/faker/internet.cr @@ -9,12 +9,12 @@ module Faker end def self.safe_email(name = nil) - [user_name(name), "example." + %w(org com net).shuffle.first].join("@") + [user_name(name), "example." + %w(org com net).shuffle(Faker.rng).first].join("@") end def self.user_name(specifier = nil, separators = %w(. _)) if specifier.is_a? String - return specifier.scan(/\w+/).map { |s| s[0] }.shuffle.join(separators.sample).downcase + return specifier.scan(/\w+/).map { |s| s[0] }.shuffle(Faker.rng).join(separators.sample(Faker.rng)).downcase elsif specifier.is_a? Int tries = 0 # Don't try forever in case we get something like 1_000_000. result = "" @@ -40,15 +40,15 @@ module Faker return result[0...specifier.max] end - return name.scan(/\w+/).shuffle.map(&.[0]).join(separators.sample).downcase if name + return name.scan(/\w+/).shuffle(Faker.rng).map(&.[0]).join(separators.sample(Faker.rng)).downcase if name [ ->{ Name.first_name.gsub(/\W/, "").downcase }, ->{ [Name.first_name, Name.last_name].map { |n| n.gsub(/\W/, "") - }.join(separators.sample).downcase + }.join(separators.sample(Faker.rng)).downcase }, - ].sample.call + ].sample(Faker.rng).call end def self.domain_name @@ -65,22 +65,22 @@ module Faker def self.ip_v4_address [ - (2..254).to_a.sample, - (2..254).to_a.sample, - (2..254).to_a.sample, - (2..254).to_a.sample, + (2..254).to_a.sample(Faker.rng), + (2..254).to_a.sample(Faker.rng), + (2..254).to_a.sample(Faker.rng), + (2..254).to_a.sample(Faker.rng), ].join('.') end def self.ip_v6_address ip_v6_space = (0..65535).to_a - container = (1..8).map { |_| ip_v6_space.sample } + container = (1..8).map { |_| ip_v6_space.sample(Faker.rng) } container.map { |n| n.to_s(16) }.join(':') end def self.mac_address(prefix = "") prefix_digits = prefix.split(":").map { |d| d.to_i?(16) ? d.to_i?(16) : 0 } - address_digits = (1..(6 - prefix_digits.size)).map { rand(256) } + address_digits = (1..(6 - prefix_digits.size)).map { Faker.rng.rand(256) } (prefix_digits + address_digits).map { |d| "%02x" % d }.join(":") end @@ -89,7 +89,7 @@ module Faker end def self.slug(words = nil, glue = nil) - glue ||= %w(- _ .).sample + glue ||= %w(- _ .).sample(Faker.rng) (words || Lorem.words(2).join(' ')).gsub(' ', glue).downcase end @@ -97,7 +97,7 @@ module Faker temp = Lorem.characters(min_length) diff_length = max_length - min_length if diff_length > 0 - diff_rand = rand(diff_length + 1) + diff_rand = Faker.rng.rand(diff_length + 1) temp += Lorem.characters(diff_rand) end temp = temp[0..min_length] if min_length > 0 @@ -108,8 +108,8 @@ module Faker if special_chars chars = %w(! @ # $ % ^ & *) - Random.rand(min_length).times do |i| - temp = temp.sub({temp[i] => chars[Random.rand(chars.size)]}) + Faker.rng.rand(min_length).times do |i| + temp = temp.sub({temp[i] => chars[Faker.rng.rand(chars.size)]}) end end diff --git a/src/faker/lorem.cr b/src/faker/lorem.cr index 500b083..f69711f 100644 --- a/src/faker/lorem.cr +++ b/src/faker/lorem.cr @@ -7,7 +7,7 @@ module Faker def self.characters(char_count = 255) chars = ("a".."z").to_a + (0..9).to_a - Array(String).new(char_count < 0 ? 0 : char_count, "").map { chars.sample }.join("") + Array(String).new(char_count < 0 ? 0 : char_count, "").map { chars.sample(Faker.rng) }.join("") end def self.word @@ -19,11 +19,11 @@ module Faker words = Data["lorem"]["words"].as Array(String) words += (Data["lorem"]["supplemental"].as Array(String)) if supplemental - words.shuffle[0, num] + words.shuffle(Faker.rng)[0, num] end def self.sentence(word_count = 4, supplemental = false, random_words_to_add = 6) - words(word_count + rand(random_words_to_add.to_i).to_i, supplemental).join(" ").capitalize + "." + words(word_count + Faker.rng.rand(random_words_to_add.to_i).to_i, supplemental).join(" ").capitalize + "." end def self.sentences(sentence_count = 3, supplemental = false) @@ -35,7 +35,7 @@ module Faker end def self.paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3) - sentences(sentence_count + rand(random_sentences_to_add.to_i).to_i, supplemental).join(" ") + sentences(sentence_count + Faker.rng.rand(random_sentences_to_add.to_i).to_i, supplemental).join(" ") end def self.paragraphs(paragraph_count = 3, supplemental = false) diff --git a/src/faker/number.cr b/src/faker/number.cr index 542e504..1f68446 100644 --- a/src/faker/number.cr +++ b/src/faker/number.cr @@ -5,7 +5,7 @@ module Faker end def self.digit - rand(10).to_s + Faker.rng.rand(10).to_s end def self.decimal(l_digits, r_digits = 2) @@ -30,7 +30,7 @@ module Faker def self.hexadecimal(digits) hex = "" - digits.times { hex += rand(15).to_s(16) } + digits.times { hex += Faker.rng.rand(15).to_s(16) } hex end