Skip to content

Instantly share code, notes, and snippets.

@thomasfl
Last active March 16, 2016 08:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasfl/51f6788c9f5e6045313d to your computer and use it in GitHub Desktop.
Save thomasfl/51f6788c9f5e6045313d to your computer and use it in GitHub Desktop.
# All purpose minimalistic random generator
def random(params)
if(params.kind_of?(Array))
return params[rand(params.size)]
end
if(params.kind_of?(Range))
return (rand(params.end - params.begin) + params.begin).to_s
end
if(params.kind_of?(Integer))
value = ""
params.times do
value << (48 + rand(10)).chr
end
return value
end
if(params.kind_of?(TrueClass) or params.kind_of?(FalseClass))
return (rand(2) == 0)
end
end
def test_random
print "Testing random helper "
assert_equals("Should pick random element from array",
random(['one','two','thr']).length, 3)
assert_equals("should generate numbers between range",
random(10..99).to_s.length, 2)
assert_equals("should generate a n digit number",
random(6).length, 6)
b = random(true)
assert_equals("Should generate boolean value",
(b == true || b == false), true)
puts "All tests passed."
end
def assert_equals(message, expected, actual)
print "."
if(expected.to_s != actual.to_s)
puts "Assert error: #{message}"
puts "Expected #{expected} got #{actual}"
exit(1)
end
end
# test_random() # Uncomment to test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment