需要得到一个大小写混合的字符串[a,A-Z,z],目前用的方法是
value = ""; 12.times{value << ((rand(2)==1?65:97) + rand(26)).chr}
感觉还是不好,而且再加混合数字的串就不会了。不知道有没有更好的方法,烦请详细指点!
小伙看你根骨奇佳,潜力无限,来学PHP伐。
(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
The following is more efficient: o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten; string = (0...50).map{ o[rand(o.length)] }.join;
o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten; string = (0...50).map{ o[rand(o.length)] }.join;
Add one more for the poster to choose from:rand(36 ** n).to_s(36) n is the length of the generated string
rand(36 ** n).to_s(36)
[*('a'..'z'),*('A'..'Z'),*(0..9)].shuffle[0..9].join
(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
The following is more efficient:
o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten;
string = (0...50).map{ o[rand(o.length)] }.join;
Add one more for the poster to choose from:
rand(36 ** n).to_s(36)
n is the length of the generated string
[*('a'..'z'),*('A'..'Z'),*(0..9)].shuffle[0..9].join