如何在Julia中生成随机的字母数字字符串?

编程入门 行业动态 更新时间:2024-10-28 20:26:10
本文介绍了如何在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中生成随机的字母数字字符串?

本文发布于:2023-10-17 12:42:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字母   数字   如何在   Julia

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!