Module: Netrand

Defined in:
lib/functions/int.rb,
lib/functions/dice.rb,
lib/functions/list.rb,
lib/functions/common.rb,
lib/functions/string.rb,
lib/functions/sequence.rb

Defined Under Namespace

Classes: QuotaExhaustedException

Class Method Summary collapse

Class Method Details

.check_quotaObject

Generates exception if quota of random.org is exhausted.



5
6
7
8
9
10
11
12
# File 'lib/functions/common.rb', line 5

def self.check_quota
  quota = nil
  open("http://www.random.org/quota/?format=plain") do |c|
    quota = c.read.to_i
    self.check_quota_value(quota)
  end
  quota
end

.dice(dices) ⇒ Object

Simulates dice rolls. Example:
Netrand.dice(10)
Can return array like this: [2, 4, 6, 3, 1, 5, 3, 2]



7
8
9
# File 'lib/functions/dice.rb', line 7

def self.dice(dices)
  self.int(dices, 6)
end

.int(numget, max, min = 1, uniq = :notuniq) ⇒ Object

Generates numget random numbers using random.org service. You must set maximum and can set minimum number. Returns Array of numbers. If you use :unique flag, then there won’t be two same numbers in the array.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
# File 'lib/functions/int.rb', line 9

def self.int(numget, max, min = 1, uniq = :notuniq)
  raise ArgumentError, "You must ask for more numbers than 0" if numget == 0
  raise ArgumentError, "Maximum number must be bigger than 0" if max == 0
  raise ArgumentError, "max must be bigger than min!"         if min > max
  self.check_quota
  values = self.harvest("http://www.random.org/integers/?num=#{numget}&min=#{min}&max=#{max}&format=plain&col=1&base=10")
  if uniq == :unique then self.get_unique_ints(numget, max, min, values) end
  values
end

.list(arr) ⇒ Object

Randomizes order of items in array Array must be shorter than 10 000 items Example:
Netrand.list([1, 2, 3])
May return [2, 1, 3]



10
11
12
13
14
15
16
17
18
# File 'lib/functions/list.rb', line 10

def self.list(arr)
  randarr = []
  random_positions = self.sequence(arr.length - 1, 0)
  arr = arr.to_enum
  random_positions.each do |pos|
    randarr[pos] = arr.next
  end
  randarr
end

.randomize_file_lines(filename) ⇒ Object

Randomizes order of lines in file They are saved to file called input_file_name-randomized.type



23
24
25
26
27
28
29
30
31
# File 'lib/functions/list.rb', line 23

def self.randomize_file_lines(filename)
  src = File.new(filename, "r")
  out = File.new(assemble_randomized_path(src), "w")
  values = src.readlines
  randomized_values = self.list(values)
  randomized_values.each {|value| out.write(value)}
  src.close
  out.close
end

.sequence(max, min = 1) ⇒ Object

Returns sequence of numbers from min to max in random order.



5
6
7
8
9
10
# File 'lib/functions/sequence.rb', line 5

def self.sequence(max, min = 1)
  if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
  if min > max then raise ArgumentError, "max must be bigger than min!" end
  self.check_quota
  order = self.harvest("http://www.random.org/sequences/?min=#{min}&max=#{max}&col=1&format=plain&rnd=new")
end

.string(num, len, *opts) ⇒ Object

Generates num random strings of len length. They can contain digits, uppercase and lowercase letters. They can be set to be unique. Example:
Netrand.string(10, 5, :upperalpha, :loweralpha, :digits, :unique)
Will produce 10 5 characters long strings containing uppercase, lowercase letters and digits. There won’t be 2 same characters in one string. This example used all of the possible options



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/functions/string.rb', line 12

def self.string(num, len, *opts)
  digits      = opts.include?(:digits)      ? "on" : "off"
  upperalpha  = opts.include?(:upperalpha)  ? "on" : "off"
  loweralpha  = opts.include?(:loweralpha)  ? "on" : "off"
  unique      = opts.include?(:unique)      ? "on" : "off"
  if len == 0 then raise ArgumentError, "Strings must be longer than 0 characters." end
  if digits == "off" && upperalpha == "off" && loweralpha == "off" then raise ArgumentError, "You disabled every type of character." end
  values = Array.new
  self.check_quota
  open("http://www.random.org/strings/?num=#{num}&len=#{len}&digits=#{digits}&upperalpha=#{upperalpha}&loweralpha=#{loweralpha}&unique=#{unique}&format=plain") do |c|
    c.each_line {|l| values << l.chop}
  end
  values
end