Module: Racket::Misc

Defined in:
lib/racket/misc/lv.rb,
lib/racket/misc/vt.rb,
lib/racket/misc/tlv.rb,
lib/racket/misc/misc.rb,
lib/racket/misc/orderedhash.rb

Defined Under Namespace

Classes: LV, OrderedHash, TLV, VT

Class Method Summary collapse

Class Method Details

.randbits(size) ⇒ Object

Return a number that is at most size bits long



31
32
33
34
35
36
37
38
39
# File 'lib/racket/misc/misc.rb', line 31

def Misc.randbits(size)
  bits = 0
  srand Time.now.usec
  0.upto(size-1) {
    bits <<= 1
    bits |= rand(2)
  }
  bits
end

.randbytes(size) ⇒ Object

Return a byte that is at most size bytes long



42
43
44
45
46
47
48
49
# File 'lib/racket/misc/misc.rb', line 42

def Misc.randbytes(size)
  bytes = 0
  0.upto(size-1) {
    bytes <<= 8
    bytes |= randbits(8)
  }
  bytes
end

.randstring(size) ⇒ Object

Return a string that is at most size characters long



52
53
54
55
56
57
58
# File 'lib/racket/misc/misc.rb', line 52

def Misc.randstring(size)
  s = ""
  0.upto(size-1) {
    s += sprintf("%c", randbytes(1))
  }
  s
end