Class: Gap::Random

Inherits:
Object
  • Object
show all
Defined in:
lib/gap50/samsara/random.rb

Constant Summary collapse

REG =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRandom

Returns a new instance of Random.



21
22
23
24
25
26
# File 'lib/gap50/samsara/random.rb', line 21

def initialize
    buf = "\0" * 4
    CR_ACQUIRE.call buf, 0, 0, 1, -268435456
    @ptr, = buf.unpack("L")
    self.class.register_auto_release object_id, @ptr
end

Class Method Details

._dispose(ptr) ⇒ Object



42
43
44
45
46
# File 'lib/gap50/samsara/random.rb', line 42

def self._dispose(ptr)
    if ptr
        CR_RELEASE.call ptr, 0
    end
end

.auto_release_procObject



7
8
9
10
11
12
13
14
# File 'lib/gap50/samsara/random.rb', line 7

def self.auto_release_proc
    proc{|id|
        if REG.include? id
            _dispose REG[id]
            REG.delete id
        end
    }
end

.register_auto_release(id, ptr) ⇒ Object



16
17
18
19
# File 'lib/gap50/samsara/random.rb', line 16

def self.register_auto_release(id, ptr)
    REG[object_id] = ptr
    ObjectSpace.define_finalizer self, auto_release_proc
end

Instance Method Details

#bytes(len = 16) ⇒ Object



28
29
30
31
32
# File 'lib/gap50/samsara/random.rb', line 28

def bytes(len = 16)
    buf = "\0" * len
    CR_GENRANDOM.call @ptr, len, buf
    buf
end

#disposeObject



48
49
50
51
# File 'lib/gap50/samsara/random.rb', line 48

def dispose
   self.class._dispose @ptr
   @ptr = nil
end

#hex(len = 16) ⇒ Object



34
35
36
# File 'lib/gap50/samsara/random.rb', line 34

def hex(len = 16)
    bytes(len).unpack("H*").first
end

#next_intObject



38
39
40
# File 'lib/gap50/samsara/random.rb', line 38

def next_int
    bytes(4).unpack("L").first
end