加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在Perl中,如何生成由八个十六进制数字组成的随机字符串?

发布时间:2020-12-15 21:51:04 所属栏目:大数据 来源:网络整理
导读:使用Perl,不使用任何不带ActivePerl的额外模块,如何从0-F创建一个包含8个字符的字符串.例如0F1672DA?填充应该是可控的,最好是8个字符. 我想要生成的字符串种类的更多示例: 28DA978255C7128A 解决方法 原则上,你应该能做到 #!/usr/bin/env perluse strict;
使用Perl,不使用任何不带ActivePerl的额外模块,如何从0-F创建一个包含8个字符的字符串.例如0F1672DA?填充应该是可控的,最好是8个字符.

我想要生成的字符串种类的更多示例:

28DA9782
55C7128A

解决方法

原则上,你应该能做到
#!/usr/bin/env perl

use strict; use warnings;

for (1 .. 10) {
    printf "%08Xn",rand(0xffffffff);
}

但是,您可能会发现 – 至少在某些系统上有一些perls(如果不是全部) – range of rand is restricted to 32,768 values.

您还可以学习String::Random的源代码,以了解如何生成满足其他条件的随机字符串.

但是,我不建议在Windows系统上使用内置兰特.有关高质量RNG,请参阅Math::Random::MT.

#!/usr/bin/env perl

use strict; use warnings;

my @set = ('0' ..'9','A' .. 'F');
my $str = join '' => map $set[rand @set],1 .. 8;
print "$strn";

PS:The issue with Perl’s rand on Windows was fixed in 5.20:

This meant that the quality of perl’s random numbers would vary from platform to platform,from the 15 bits of rand() on Windows to 48-bits on POSIX platforms such as Linux with drand48().

Perl now uses its own internal drand48() implementation on all platforms. This does not make perl’s rand cryptographically secure. [perl #115928]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读