使用Perl,不使用任何不带ActivePerl的额外模块,如何从0-F创建一个包含8个字符的字符串.例如0F1672DA?填充应该是可控的,最好是8个字符.
我想要生成的字符串种类的更多示例:
28DA9782 55C7128A
解决方法
原则上,你应该能做到
#!/usr/bin/env perl use strict; use warnings; for (1 .. 10) { printf "%08X\n",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 "$str\n";
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]