(0..2).each do |i| j = rand(u.size - i) + i if i != j u[i] += u[j]; u[j] = u[i] - u[j]; u[i] -= u[j]; end p u[i]end
(0..2).each do |i| j = rand(u.size - i) + i if i != j u[i] += u[j]; u[j] = u[i] - u[j]; u[i] -= u[j]; end p u[i] end
RyanBern 发表于 2015-9-7 09:17 仅要两个数字的情况(实际是模拟shuffle的前两步),如果要生成多个请把第一行那个2改掉: (0..2).each do ...
a, b = [2,3,5,6,7,8].sample(2)
taroxd 发表于 2015-9-7 18:30 a, b = [2,3,5,6,7,8].sample(2)[/pre]