ruby 随机数
rand(max=0) => number
-----------------------------------------------------------------------
Converts max to an integer using max1 = max.to_i.abs. If the result is zero, returns a pseudorandom floating point number greater than or equal to 0.0 and less than 1.0. Otherwise, returns a pseudorandom integer greater than or equal to zero and less than max1. Kernel::srand may be used to ensure repeatable sequences of random numbers between different runs of the program. Ruby currently uses a modified Mersenne Twister with a period of 2**19937-1 .
srand 1234 #=> 0
[ rand, rand ] # => [0.191519450163469, 0.49766366626136 ]
[ rand(10), rand(1000) ] #=> [6, 817 ]
srand 1234 #=> 1234
[ rand, rand ] # => [0.191519450163469, 0.49766366626136 ]
print 'Enter a guess : '
guess= gets.chomp.to_i
until guess==num do
if guess< num
puts 'Too low'
elsif guess> num
puts 'Too high'
end
print 'Enter a guess: '
guess= gets.chomp.to_i
end
puts "Good job,the number was #{num}!"
from http://ajax.suaccess.org/ruby/ruby-generating-random-numbers/
Use the Kernel#rand function with no arguments to select a psuedorandom floating-point number from a uniform distribution between 0 and 1.
rand # => 0.517297883846589 rand # => 0.946962603814814
Pass in a single integer argument n to Kernel#rand, and it returns an integer between 0 and n-1:
rand(5) # => 0 rand(5) # => 4 rand(5) # => 3 rand(1000) # => 39
Discussion
You can use the single-argument form of Kernel#rand to build many common tasks based on randomness. For instance, this code selects a random item from an array.
a = ['item1', 'item2', 'item3'] a[rand(a.size)] # => "item3"
To select a random key or value from a hash, turn the keys or values into an array and select one at random.
m = { :key1 => 'value1', :key2 => 'value2', :key3 => 'value3' } values = m.values values[rand(values.size)] # => "value1"
This code generates pronounceable nonsense words:
def random_word letters = { ?v => 'aeiou', ?c => 'bcdfghjklmnprstvwyz' } word = '' 'cvcvcvc'.each_byte do |x| source = letters[x] word << source[rand(source.length)].chr end return word end random_word # => "josuyip" random_word # => "haramic"
The Ruby interpreter initializes its random number generator on startup, using a seed derived from the current time and the process number. To reliably generate the same random numbers over and over again, you can set the random number seed manually by calling the Kernel#srand function with the integer argument of your choice. This is useful when you’re writing automated tests of “random” functionality:
#Some random numbers based on process number and current time rand(1000) # => 187 rand(1000) # => 551 rand(1000) # => 911 #Start the seed with the number 1 srand 1 rand(1000) # => 37 rand(1000) # => 235 rand(1000) # => 908 #Reset the seed to its previous state srand 1 rand(1000) # => 37 rand(1000) # => 235 rand(1000) # => 908
____________________________________________________________________
from http://kajeaw.spaces.live.com/blog/cns!fbbaf568162edc32!694.entry
Ruby - Generates a random password
## Generates a random password _len_ chars long skipping commonly misread characters (ex. 0/o/O1/i/I)
def generate_password( len=6 )
chars = ("a".."h").to_a + ("j".."n").to_a + ("p".."z").to_a + ("A".."H").to_a + ("J".."N").to_a + ("P".."Z").to_a + (1..9).to_a
newpass = ""
1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
return newpass
end
__________________________________________________
from http://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/function.html#rand
srand([seed])
Sets the random number seed for the rand
. If seed is omitted, uses the current time etc. as a seed.
rand(max)
Returns a random integer number greater than or equal to 0 and less than the value of max. (max should be positive.) Automatically calls srand
unless srand()
has already been called.
If max is 0, rand
returns a random float number greater than or equal to 0 and less than 1.
_____________________________________________________
from http://www.openforge.cn/html/kaiyuanjishu/Rubyyuyan/20071003/17381.html
为什么rand总是生成相同的随机数?
在ruby 1.4.2以前的版本中, 每次执行程序时rand都会生成相同的随机数。为了生成不同的随机数, 每次都必须使用srand来给出不同的随机数种子。若不带参数地调用srand的话, 它将使用当时的时间作为种子, 因此可以产生出不同的随机数。 中国开源社区www.openforge.cn
7.3 怎样从0到51中选出5个不重复的随机数呢?
下面的方法可以从0到n中选出m个随机数,并以数组的形式返回结果。
中国开源社区www.openforge.cn
def sample(n, m) if m.zero? [] else s = sample(n-1, m-1) t = rand(n 1) s.concat s.include?(t) ? [n] : [t] end end
若不使用递归的话, 可以写成这样。
def sample(n, m) s = [] ((n-m)...n).each do |j| t = rand(j 2) s.concat s.include?(t) ? [j 1] : [t] end s end 7.4 Fixnum、Symbol、true、nil和false这些立即值与引用有什么不同?
不能定义特殊方法。 中国开源社区www.ossforge.com
另外, 表示同一个数字的Fixnum的实例通常都是相同的, 所以定义实例变量时, 它们的所指也是相同的。
_________________________________________________________
如何查看rand的源代码?