Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created September 12, 2014 09:57
Show Gist options
  • Select an option

  • Save thinkphp/5e85d98960220f8a88f3 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/5e85d98960220f8a88f3 to your computer and use it in GitHub Desktop.
A dead simple ruby that generates a file.out with randomly integers between LI and LS without duplicates.
class Generate
def initialize(filename)
@filename = filename
end
def between(num, li, ls)
@howmuch = num
@li = li
@ls = ls
end
def solve()
@generatedNumbers = gen(@howmuch, @li, @ls)
end
def writeToFile()
f = File.open(@filename,"w")
(0..@howmuch).each do |i|
f.write(@generatedNumbers[i])
f.write(" ")
end
f.close()
end
def gen(num, li, ls)
r = Random.new
taken = []
(1..num).each do |i|
n = r.rand(li..ls)
while taken.index( n )
n = r.rand(li..ls)
end
taken.push(n)
end
return taken
end
end
num, li, ls = ARGV[0].to_i, ARGV[1].to_i, ARGV[2].to_i
ob = Generate.new("input.in")
ob.between(num, li, ls)
ob.solve()
ob.writeToFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment