Class: TestBench::Random

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

Direct Known Subclasses

Substitute::Random

Defined Under Namespace

Modules: Controls, Defaults, Substitute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#generatorObject



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

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

#namespaceObject



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

def namespace
  @namespace ||= Defaults.namespace
end

#seedObject



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

def seed
  @seed ||= Defaults.seed
end

#sequenceObject



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

def sequence
  @sequence ||= 0
end

Class Method Details

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



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

def self.build(seed=nil, namespace: nil)
  instance = new
  instance.seed = seed
  instance.namespace = namespace
  instance
end

.configure(receiver, attr_name: nil) ⇒ Object



34
35
36
37
38
# File 'lib/test_bench/random/random.rb', line 34

def self.configure(receiver, attr_name: nil)
  attr_name ||= :random

  receiver.public_send(:"#{attr_name}=", instance)
end

.generator(seed, namespace) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/test_bench/random/random.rb', line 101

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

  seed ^= namespace_digest

  ::Random.new(seed)
end

.generator_seed(seed, namespace) ⇒ Object



109
110
111
112
# File 'lib/test_bench/random/random.rb', line 109

def self.generator_seed(seed, namespace)
  generator = generator(seed, namespace)
  generator.seed
end

.instanceObject



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

def self.instance
  @instance ||= build
end

.namespace_digest(namespace) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/test_bench/random/random.rb', line 114

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

Instance Method Details

#booleanObject



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

def boolean
  byte = next_qword.bytes.first

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

#fractionObject



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

def fraction
  self.sequence += 1

  generator.rand
end

#integerObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/test_bench/random/random.rb', line 44

def integer
  bytes = next_qword.bytes

  number = 0

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

  number
end

#next_qwordObject



82
83
84
85
86
# File 'lib/test_bench/random/random.rb', line 82

def next_qword
  self.sequence += 1

  generator.bytes(8)
end

#reset(namespace = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/test_bench/random/random.rb', line 88

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

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

  self.generator = generator
  self.namespace = namespace

  self.sequence = 0

  generator.seed
end

#reset?(namespace = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  if namespace != self.namespace
    return false
  end

  sequence.zero?
end

#stringObject



40
41
42
# File 'lib/test_bench/random/random.rb', line 40

def string
  integer.to_s(36)
end