Posted on by Kalkicode
Code Number

Generate random number in ruby

Ruby program for Generate random number . Here problem description and explanation.

# Ruby program for
# Print random number


class MyNumber 
	# Print random number in given size
	def simple_random(size) 
		number = 0
		i = 0
		r = Random.new
		max_int = (2**(0.size * 8 -2) -1)
		while (i < size) 
			# Get new rand number
			number = r.rand(0...max_int)
			print(number ,"\n")
			i += 1
		end
	end
	def random_between_range(first, last) 
		r = Random.new
		# random number
		number = r.rand(first...last)
		print(number ,"\n")
	end
end
def main() 
	obj = MyNumber.new()
	# Test Case
	obj.simple_random(3)
	# Range from 1 to 10
	obj.random_between_range(1, 10)
	# Range from 50 to 100
	obj.random_between_range(50, 100)
	# Range from 1000 to 2000
	obj.random_between_range(1000, 2000)
end
main()

Output

560019284106442335
3647006435479195124
1434646830460570668
9
81
1383

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment