如何在Ruby中生成随机混合字母和数字

时间:2022-05-10 07:21:03

I have the following code so far, this generates letters only. I am looking to get both letters and numbers

到目前为止,我有以下代码,这只生成字母。我希望得到字母和数字

#Generate random 8 digit number and output to file
    output = File.new("C:/Users/%user%/Desktop/Strings.txt", "w")

    i = 0
     while i < 500
        randomer = (0...8).map{65.+(rand(26)).chr}.join
        output << randomer
        output << "\n"
        i = i+1
     end

    output.close

5 个解决方案

#1


3  

How about this:

这个怎么样:

def random_tuple(length)
    letters_and_numbers = "abcdefghijklmnopqrstuvwxyz0123456789"
    answer = ""
    length.times { |i| answer << letters_and_numbers[rand(36)] }
    answer
end

output = ""
500.times { |i| output << random_tuple(8) + "\n" }

You could also have the function append the newline, but I think this way is more general.

你也可以让函数附加换行符,但我认为这种方式更为通用。

#2


3  

(('a'..'z').to_a + ('0'..'9').to_a).sample( 8 ).join

This is random, but will not re-use any number/letter, so it does not cover all possible strings.

这是随机的,但不会重复使用任何数字/字母,因此它不会涵盖所有可能的字符串。

#3


2  

All digits and letters are used in base 36:

基数36中使用所有数字和字母:

max = 36**8
500.times.map{ rand(max).to_s(36).rjust(8,'0') } #rjust pads short strings with '0'

#4


2  

Use SecureRandom.hex(10) his generate letters and numbers in 0-9 and a-f

使用SecureRandom.hex(10)他在0-9和a-f中生成字母和数字

or

要么

SecureRandom.base64.delete('/+=')[0, 8]

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html

#5


2  

You can do something like this

你可以做这样的事情

i = 0
while i < 500
    randomer = (1..8).map { (('a'..'z').to_a + ('0'..'9').to_a)[rand(36)] }.join
    output << randomer
    output << "\n"
    i = i+1
end

output.close

Enjoy.

请享用。

#1


3  

How about this:

这个怎么样:

def random_tuple(length)
    letters_and_numbers = "abcdefghijklmnopqrstuvwxyz0123456789"
    answer = ""
    length.times { |i| answer << letters_and_numbers[rand(36)] }
    answer
end

output = ""
500.times { |i| output << random_tuple(8) + "\n" }

You could also have the function append the newline, but I think this way is more general.

你也可以让函数附加换行符,但我认为这种方式更为通用。

#2


3  

(('a'..'z').to_a + ('0'..'9').to_a).sample( 8 ).join

This is random, but will not re-use any number/letter, so it does not cover all possible strings.

这是随机的,但不会重复使用任何数字/字母,因此它不会涵盖所有可能的字符串。

#3


2  

All digits and letters are used in base 36:

基数36中使用所有数字和字母:

max = 36**8
500.times.map{ rand(max).to_s(36).rjust(8,'0') } #rjust pads short strings with '0'

#4


2  

Use SecureRandom.hex(10) his generate letters and numbers in 0-9 and a-f

使用SecureRandom.hex(10)他在0-9和a-f中生成字母和数字

or

要么

SecureRandom.base64.delete('/+=')[0, 8]

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html

#5


2  

You can do something like this

你可以做这样的事情

i = 0
while i < 500
    randomer = (1..8).map { (('a'..'z').to_a + ('0'..'9').to_a)[rand(36)] }.join
    output << randomer
    output << "\n"
    i = i+1
end

output.close

Enjoy.

请享用。