如何在Julia中生成随机的字母数字字符串? [英] How to generate random alphanumeric string in julia?

查看:62
本文介绍了如何在Julia中生成随机的字母数字字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码段在julia中生成12个字符的字母数字字符串:
a)
an = randstring(rand(Bool) ? ('A':'Z') : ('0':'9'), 12)
b)

I am trying to generate 12 characters alphanumeric string in julia with the following snippets:
a)
an = randstring(rand(Bool) ? ('A':'Z') : ('0':'9'), 12)
b)

an = "" 
for i in [1:12]
    an *= randstring(rand(Bool) ? ('A':'Z') : ('0':'9'))
end

但是两者都给出完整的12位数字或12个字母,但不给出它们的组合.

but both gives either complete 12 digits or 12 alphabets but not of their combination.

请指导我生成12个字母数字字符串的组合.

please guide me in generating combination of 12 alphanumeric string.

推荐答案

如果您不介意同时使用大写和小写字母,则可以简单地调用randstring(12):

If you don't mind using both upper and lower case letters, you can simply call randstring(12):

julia> using Random

julia> randstring(12)
"0IPrGg0JVONT"

julia> randstring(12)
"EB5dhw4LVno7"

如果只需要大写字母和数字,则需要传递randstring仅包含大写字母和数字的集合,您可以使用['A':'Z'; '0':'9']来实现:

If you want only uppercase letters (and numbers), then you need to pass randstring a collection that includes only uppercase letters and numbers, which you can achieve with ['A':'Z'; '0':'9']:

julia> randstring(['A':'Z'; '0':'9'], 12)
"ASZQAT5YX3OL"

julia> randstring(['A':'Z'; '0':'9'], 12)
"FEV5HTGMLQ6X"

最后,请注意,您可以将字符集合作为字符串提供:

Finally, note that you could provide the collection of characters as a string:

julia> randstring("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 12)
"ASZQAT5YX3OL"

这篇关于如何在Julia中生成随机的字母数字字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆