我使用securerandom
,問題是,它是包括數字"0"
,如0..56
,和0..46
而不是1..56
和1..46
(彩票千萬不包括0位)。在下面的第二個兆數輸出中可以看到0。我想我想出了一個方法來排除0
s(char1
,char2
),但我失敗了。試圖使用一個包含範圍與SecureRandom.random_number紅寶石2.0.0
當我使用rand(1..56)
與包容性範圍它工作得很好(但幾個職位建議遠離rand()
)。但是,當我嘗試使用char1
和char2
以下securerandom
包含範圍它不起作用。
Error: [[email protected] lottery]$ ./mega_millions.rb /dev/null/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/securerandom.rb:216:in `<': comparison of Fixnum with Array failed (ArgumentError) from /dev/null/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/securerandom.rb:216:in `random_number' from ./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}"
[[email protected] lottery]$ ./mega_millions.rb
34, 38, 17, 24, 55, 14
[[email protected] lottery]$ ./mega_millions.rb
30, 26, 7, 4, 32, 0
感謝您的快速響應。 Def幫助我! – nanotechz9l 2013-04-25 04:09:05