Class: TestBench::Random

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/random/random.rb,
lib/test_bench/random/controls/seed.rb

Defined Under Namespace

Modules: Controls, Defaults

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed, default_namespace) ⇒ Random

Returns a new instance of Random.



11
12
13
14
# File 'lib/test_bench/random/random.rb', line 11

def initialize(seed, default_namespace)
  @seed = seed
  @default_namespace = default_namespace
end

Instance Attribute Details

#default_namespaceObject (readonly)

Returns the value of attribute default_namespace.



9
10
11
# File 'lib/test_bench/random/random.rb', line 9

def default_namespace
  @default_namespace
end

#generatorObject



3
4
5
# File 'lib/test_bench/random/random.rb', line 3

def generator
  @generator ||= self.class.generator(seed, default_namespace)
end

#seedObject (readonly)

Returns the value of attribute seed.



8
9
10
# File 'lib/test_bench/random/random.rb', line 8

def seed
  @seed
end

Class Method Details

.booleanObject



30
# File 'lib/test_bench/random/random.rb', line 30

def self.boolean = instance.boolean

.build(seed = nil, namespace: nil) ⇒ Object



16
17
18
19
20
21
# File 'lib/test_bench/random/random.rb', line 16

def self.build(seed=nil, namespace: nil)
  seed ||= Defaults.seed
  namespace ||= Defaults.namespace

  new(seed, namespace)
end

.fractionObject



29
# File 'lib/test_bench/random/random.rb', line 29

def self.fraction = instance.fraction

.generator(seed, namespace) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/test_bench/random/random.rb', line 71

def self.generator(seed, namespace)
  namespace_digest = namespace_digest(namespace)

  seed ^= namespace_digest

  ::Random.new(seed)
end

.instanceObject



23
24
25
# File 'lib/test_bench/random/random.rb', line 23

def self.instance
  @instance ||= build
end

.integerObject



28
# File 'lib/test_bench/random/random.rb', line 28

def self.integer = instance.integer

.namespace_digest(namespace) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/test_bench/random/random.rb', line 79

def self.namespace_digest(namespace)
  byte_width = 8

  sequence = namespace.bytes.each_slice(byte_width).map do |bytes|
    value = 0

    bytes.each_with_index do |byte, index|
      value <<= 8 if not index.zero?

      value += byte
    end

    value
  end

  digest_salt = (0x100 ** byte_width) - 1
  sequence.push(digest_salt)

  sequence.reduce do |checksum, value|
    checksum ^ value
  end
end

.resetObject



31
# File 'lib/test_bench/random/random.rb', line 31

def self.reset(...) = instance.reset(...)

.stringObject



27
# File 'lib/test_bench/random/random.rb', line 27

def self.string = instance.string

Instance Method Details

#booleanObject



53
54
55
56
57
58
59
60
61
# File 'lib/test_bench/random/random.rb', line 53

def boolean
  byte = generator.bytes(1).bytes.first

  if byte % 2 == 1
    true
  else
    false
  end
end

#fractionObject



49
50
51
# File 'lib/test_bench/random/random.rb', line 49

def fraction
  generator.rand
end

#integerObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/test_bench/random/random.rb', line 37

def integer
  bytes = generator.bytes(8).bytes

  number = 0

  bytes.each_with_index do |byte, index|
    number += byte ** index
  end

  number
end

#reset(namespace = nil) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/test_bench/random/random.rb', line 63

def reset(namespace=nil)
  namespace ||= self.default_namespace

  generator = self.class.generator(seed, namespace)

  self.generator = generator
end

#stringObject



33
34
35
# File 'lib/test_bench/random/random.rb', line 33

def string
  integer.to_s(36)
end