Class: ForemanNameGenerator::RandomGenerator
- Defined in:
- lib/foreman_name_generator/random_generator.rb
Constant Summary collapse
- MASK =
0x1ffffff
Constants inherited from Generator
Generator::DEFAULT_DATA_DIR, Generator::GIVEN_FEMALE_NAMES_FILE, Generator::GIVEN_MALE_NAMES_FILE, Generator::RECORD_LENGTH_GIVEN, Generator::RECORD_LENGTH_SURNAME, Generator::SURNAMES_FILE
Class Method Summary collapse
Instance Method Summary collapse
- #generate(seed = Time.now.utc.to_i) ⇒ Object
-
#next_lfsr25(seed) ⇒ Object
Fibonacci linear feedback shift register with x^25 + x^24 + x^23 + x^22 + 1 poly.
Methods inherited from Generator
Constructor Details
This class inherits a constructor from ForemanNameGenerator::Generator
Class Method Details
.random_initial_seed ⇒ Object
31 32 33 |
# File 'lib/foreman_name_generator/random_generator.rb', line 31 def self.random_initial_seed rand(MASK - 2) + 1 end |
Instance Method Details
#generate(seed = Time.now.utc.to_i) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/foreman_name_generator/random_generator.rb', line 16 def generate(seed = Time.now.utc.to_i) return [] if seed.nil? || seed == 0 index = seed loop do index = next_lfsr25(index) break if unique?(seed, index) end given_file = (index & 0x1000000) == 0 ? GIVEN_MALE_NAMES_FILE : GIVEN_FEMALE_NAMES_FILE givenname_ix = (index & 0xff0000) >> 16 surnname_ix = index & 0xffff firstname = find_name(givenname_ix, data_file(given_file), RECORD_LENGTH_GIVEN) surname = find_name(surnname_ix, data_file(SURNAMES_FILE), RECORD_LENGTH_SURNAME) [index, firstname, surname] end |
#next_lfsr25(seed) ⇒ Object
Fibonacci linear feedback shift register with x^25 + x^24 + x^23 + x^22 + 1 poly
6 7 8 9 10 11 12 13 14 |
# File 'lib/foreman_name_generator/random_generator.rb', line 6 def next_lfsr25(seed) i = 1 i = (seed + 1) & MASK raise ArgumentError, "Seed #{seed} out of bounds" if seed && i == 0 i = (seed + 1) & MASK while i == 0 i = (i >> 1) | ((i[0]^i[1]^i[2]^i[3]) << 0x18) i = (i >> 1) | ((i[0]^i[1]^i[2]^i[3]) << 0x18) while i > MASK i - 1 end |