How can I produce a random number in a range from 1 million to 10 million?
rand(10) works, I tried rand(1..10) and that didn't work.
Take your base number, 1,000,000 and add a random number from 0 up to your max - starting number:
1_000_000 + Random.rand(10_000_000 - 1_000_000) #=> 3084592 require 'random', and also checking if it's 1.9.2 only?rand, not Random.rand. It works all the way back as far as 1.6.0It's an instance method:
puts Random.new.rand(1_000_000..10_000_000-1) 1_000_000...10_000_000 is more Ruby-ish.Or, in case performance is not an issue and you don't want to count zeros:
(0...7).map { |i| rand((i == 0 ? 1 : 0)..9) }.join.to_i