Skip to content

@cheapRoc /random_phones.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#
# generates a random phone number by generating a random set
#
require 'set'
class RandomPhone
PHONE_MAP = []
def self.number
nums = rand_n(10, 10)
"#{nums[0..2].join}-#{nums[3..5].join}-#{nums[6..9].join}"
end
protected
def self.rand_n(n, max)
randoms = []
loop do
break randoms = gen_rand(n, max)
end while PHONE_MAP.include?(randoms.join) or randoms.empty?
PHONE_MAP << randoms.join
return randoms
end
def self.gen_rand(n, max)
randoms = Set.new
loop do
randoms << rand(max)
return randoms.to_a if randoms.size >= n
end
end
end
@cheapRoc
Owner

This isn't perfect but fit the need, don't use this in your app code unless you like memory growing outrageously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.