Số ngẫu nhiên
Ruby có sẵn một phương thức tạo số ngẫu nhiên: rand. Bạn sẽ nhận được một số thực bất kì lớn hơn hoặc bằng 0 và nhỏ hơn 1.0. Nếu bạn truyền vào rand tham số là một số nguyên (ví dụ rand(5)), bạn sẽ nhận được giá trị nguyên lớn hơn hoặc bằng 0, và nhỏ hơn tham số.
Xem ví dụ p026phrase.rb dưới đây:
=begin
If you call rand, you'll get a float greater than or equal to 0.0
and less than 1.0. If you give it an integer parameter (by calling rand(5) ),
you will get an integer value greater than or equal to 0 and less than 5
=end
# The program below makes three lists of words, and then randomly picks one word
# from each of the three lists and prints out the result
word_list_one = ['24/7', 'multi-Tier', '30,000 foot', 'B-to-B', 'win-win', 'front-end',
'web-based', 'pervasive', 'smart', 'six-sigma', 'critical-path', 'dynamic']
word_list_two = ['empowered', 'sticky', 'value-added', 'oriented', 'centric', 'distributed',
'clustered', 'branded', 'outside-the-box', 'positioned', 'networked', 'focused',
'leveraged', 'aligned', 'targeted', 'shared', 'cooperative', 'accelerated']
word_list_three = ['process', 'tipping-point', 'solution', 'architecture', 'core competency',
'strategy', 'mindshare', 'portal', 'space', 'vision', 'paradigm', 'mission']
one_len = word_list_one.length
two_len = word_list_two.length
three_len = word_list_three.length
rand1 = rand(one_len)
rand2 = rand(two_len)
rand3 = rand(three_len)
phrase = word_list_one[rand1] + " " + word_list_two[rand2] + " " + word_list_three[rand3]
puts phrase
Chương trình trên sẽ tạo ra 3 mảng chứa từ, và sau đó sẽ lấy ngẫu nhiên từ mỗi mảng 1 từ để đưa ra màn hình kết quả.
n_leminh said,
Chao ban! ban co the cho minh 1 doan code de connect toi MySQL database = ruby dc ko? va minh cug can 1 class test de test viec connect do lun (nhung method dung de CRUD). Mong ban giup do minh, vi minh thuc su dang bi stuck trong ruby on rails day !!!
RSS feed for comments on this post · TrackBack URI