Class: Random

Inherits:
Object show all
Defined in:
lib/moon/packages/std/core_ext/random.rb

Constant Summary collapse

BINARY_DIGITS =
%w(0 1)
OCTAL_DIGITS =
%w(0 1 2 3 4 5 6 7)
HEX_DIGITS =
%w(0 1 2 3 4 5 6 7 8 9 A B C D E F)
BASE64_DIGITS =
%w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 + -)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(data) ⇒ Random

Loads a serialized Random object

Parameters:

Returns:


87
88
89
# File 'lib/moon/packages/std/core_ext/random.rb', line 87

def self.load(data)
  new data["seed"].to_i
end

.randomRandom

Returns an instance of the Random class, this is a globally available instance.

Returns:


95
96
97
# File 'lib/moon/packages/std/core_ext/random.rb', line 95

def self.random
  @random ||= new
end

Instance Method Details

#base64(digits) ⇒ String

Generates a random base64 string.

Parameters:

  • digits (Integer)

    length of the base64 string

Returns:

  • (String)

    string generated


63
64
65
# File 'lib/moon/packages/std/core_ext/random.rb', line 63

def base64(digits)
  string(BASE64_DIGITS, digits)
end

#binary(digits) ⇒ String

Generates a random binary string.

Parameters:

  • digits (Integer)

    length of the binary string

Returns:

  • (String)

    string generated


39
40
41
# File 'lib/moon/packages/std/core_ext/random.rb', line 39

def binary(digits)
  string(BINARY_DIGITS, digits)
end

#exportHash<String, Object>

Exports the Random as a serializable object

Returns:


79
80
81
# File 'lib/moon/packages/std/core_ext/random.rb', line 79

def export
  to_h.stringify_keys
end

#hex(digits) ⇒ String

Generates a random hexa-decimal string.

Parameters:

  • digits (Integer)

    length of the hex string

Returns:

  • (String)

    string generated


55
56
57
# File 'lib/moon/packages/std/core_ext/random.rb', line 55

def hex(digits)
  string(HEX_DIGITS, digits)
end

#int(size) ⇒ Integer

Generates a random integer.

Parameters:

  • size (Integer)

    maximum size of the generate integer

Returns:


13
14
15
# File 'lib/moon/packages/std/core_ext/random.rb', line 13

def int(size)
  rand(size).to_i
end

#octal(digits) ⇒ String

Generates a random octal string.

Parameters:

  • digits (Integer)

    length of the octal string

Returns:

  • (String)

    string generated


47
48
49
# File 'lib/moon/packages/std/core_ext/random.rb', line 47

def octal(digits)
  string(OCTAL_DIGITS, digits)
end

#sample(array) ⇒ Object

Selects a random element from the Array.

Parameters:

Returns:

  • (Object)

    random element from the array


21
22
23
24
# File 'lib/moon/packages/std/core_ext/random.rb', line 21

def sample(array)
  return nil if array.empty?
  array[int(array.size)]
end

#string(chars, length) ⇒ String

Generates a random string from the given chars

Parameters:

  • chars (Array<String>)

    chars to sample from

  • length (Integer)

    length of the string to generate

Returns:

  • (String)

    generated string


31
32
33
# File 'lib/moon/packages/std/core_ext/random.rb', line 31

def string(chars, length)
  length.times.map { sample(chars) }.join('')
end

#to_hHash

Returns the current settings for Random object.

Returns:


70
71
72
73
74
# File 'lib/moon/packages/std/core_ext/random.rb', line 70

def to_h
  {
    seed: seed
  }
end