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

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

问题描述

我正在尝试使用以下片段在 julia 中生成 12 个字符的字母数字字符串:
一)
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天全站免登陆