Module: RandomData::Numbers

Included in:
Random
Defined in:
lib/random_data/numbers.rb

Instance Method Summary collapse

Instance Method Details

#bitObject

return a random bit, 0 or 1.



19
20
21
# File 'lib/random_data/numbers.rb', line 19

def bit
  rand(2)
end

#bits(n) ⇒ Object

return an array of n random bits.



24
25
26
27
28
# File 'lib/random_data/numbers.rb', line 24

def bits(n)
  x = []
  n.times {x << bit}
  x
end

#number(n) ⇒ Object

n can be an Integer or a Range. If it is an Integer, it just returns a random number greater than or equal to 0 and less than n. If it is a Range, it returns a random number within the range Examples

>> Random.number(5)

> 4

>> Random.number(5)

> 2

>> Random.number(5)

> 1



14
15
16
# File 'lib/random_data/numbers.rb', line 14

def number(n)
  n.is_a?(Range) ? n.to_a.rand : rand(n) 
end