我正在使用securerandom,问题是它包含 digit "0",如0..56, 而0..46不是1..56, and 1..46(彩票不包括 0 数字)。0 可以在下面的第二个兆数输出中看到。我以为我找到了一种0用 ( char1, char2) 排除下面 s 的方法,但我失败了。
当我使用rand(1..56)包含范围时,它工作得很好(但有几篇文章建议远离rand())。但是,当我尝试使用包含范围char1和char2以下范围时,securerandom它不起作用。
错误:
[nil@nil 彩票]$ ./mega_millions.rb
/dev/null/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/securerandom.rb:216:in `<':Fixnum 与数组的比较失败(ArgumentError)
来自 /dev/null/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/securerandom.rb:216:in `random_number'
来自 ./mega_millions.rb:42:in `<main>'
我将不胜感激任何反馈并朝着正确的方向推动。
谢谢!
#!/usr/bin/env ruby
require 'rainbow.rb'
require 'securerandom'
quick_pick = 56
char1 = ('1'..'56').to_a
mega_num = 46
char2 = ('1'..'46').to_a
first = SecureRandom.random_number(quick_pick)
second = SecureRandom.random_number(quick_pick)
third = SecureRandom.random_number(quick_pick)
fourth = SecureRandom.random_number(quick_pick)
fifth = SecureRandom.random_number(quick_pick)
mega = SecureRandom.random_number(mega_num)
puts "#{first}, #{second}, #{third}, #{fourth}, #{fifth}, #{mega}"
[nil@nil lottery]$ ./mega_millions.rb
34, 38, 17, 24, 55, 14
[nil@nil lottery]$ ./mega_millions.rb
30, 26, 7, 4, 32, 0