Class: Crypt::Xorshift64Star

Inherits:
Object
  • Object
show all
Defined in:
lib/crypt/isaac/xorshift/pure.rb,
ext/crypt/isaac/xorshift/xorshift.c

Constant Summary collapse

UINT32_C =
2**32
UINT64_C =
2**64
UINT64_Cf =
UINT64_C.to_f

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Xorshift64Star

Returns a new instance of Xorshift64Star.



21
22
23
24
25
# File 'ext/crypt/isaac/xorshift/xorshift.c', line 21

def initialize( seed = new_seed )
  @seed = nil
  srand( seed )
  @old_seed = seed
end

Instance Method Details

#==(v) ⇒ Object



106
107
108
# File 'ext/crypt/isaac/xorshift/xorshift.c', line 106

def ==(v)
  self.class == v.class && @old_seed == v.seed
end

#new_seedObject



40
41
42
43
44
45
# File 'ext/crypt/isaac/xorshift/xorshift.c', line 40

def new_seed
  n = Time.now
  @@counter ||= 0
  @@counter += 1
  [n.usec % 65536, n.to_i % 65536, $$ ^ 65536, @@counter % 65536 ].collect {|x| x.to_s(16)}.join.to_i(16)
end

#rand(args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'ext/crypt/isaac/xorshift/xorshift.c', line 77

def rand( n = 0)
  @seed ^= @seed >> 12
  @seed ^= @seed << 25
  @seed ^= @seed >> 27
  if n < 1
    ( ( @seed * 2685821657736338717 ) % UINT64_C ) / UINT64_Cf
  else
    ( ( @seed * 2685821657736338717 ) % UINT64_C ) % Integer( n )
  end
end

#seedObject



102
103
104
# File 'ext/crypt/isaac/xorshift/xorshift.c', line 102

def seed
  @old_seed
end

#srand(args) ⇒ Object



58
59
60
61
# File 'ext/crypt/isaac/xorshift/xorshift.c', line 58

def srand( seed = new_seed )
  @old_seed = @seed
  @seed = seed % UINT64_C
end