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.
I find this more readable:
7.times.map { rand(1..9) }.join.to_i
It's an instance method:
puts Random.new.rand(1_000_000..10_000_000-1)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With