问题描述
我正在尝试生成带有空格的随机字符串,但它不起作用:
I'm trying to generate random string with white spaces but it is not working :
/rs {
%i=1
%r=$rand(1,50)
%s=$rand(a,z)
while(%i <= %r) {
%i=%i+1
%s=%s $+ $rand(a,z)
if(1 == rand(1,4) %s=%s $+ $chr(32)
}
echo %s
}
返回:
WHILE(%I Unknown command
有什么想法吗?
推荐答案
您遇到了一些问题,这些只是其中的一小部分.
You had some issues, those are just few of them.
空格:当涉及到空格时,mSL 语句是有意义的,那些你应该在
while (expression)
,if (expression)
甚至%i = 1
和%r = $rand(1,50)
etc'
Spaces: mSL statements are sensebile when it concerns to spaces, those you should put spaces between the
while (expression)
,if (expression)
and even%i = 1
and%r = $rand(1,50)
etc'
括号:您可能忘记了空间生成器条件下的小括号.应该是 if (1 == rand(1,4)) %s=%s $+ $chr(32)
Parenthesis: You probably have forgotten the little parenthesis at the space generator condition. Should be if (1 == rand(1,4)) %s=%s $+ $chr(32)
$ 符号:您还忘记在此标识符之前放置此符号 rand(1,4)
应该是 $rand(1,4)
$ sign: You also forgotten to put this sign before this identifier rand(1,4)
should be $rand(1,4)
固定片段:
rs {
%i = 1
%r = $rand(1,50)
%s = $rand(a,z)
while (%i <= %r) {
%i = %i + 1
%s = %s $+ $rand(a,z)
if (1 == $rand(1,4)) %s = %s $chr(32)
}
echo -ag %s
}
我冒昧地设计了一些不同的代码,现在您可以将其用作 $identifier
而不是别名,这将为您提供更大的灵活性和可用性.
I took the liberty of designing the code a bit different, now you can use it as $identifier
instead of an alias, which will give you further flexibility and usability.
用法:
echo -ag $rs
(默认长度为 50 个字符)echo -ag $rs(20)
(长度为 20 个字符的随机字符串)set %myName $rs(15)
(将输出的随机字符串保存到一个常量变量中)
echo -ag $rs
(default will be 50 characters long)echo -ag $rs(20)
(random string with length of 20 charcathers)set %myName $rs(15)
(will save the output random string into a constant variable)
片段:
rs {
if (!$1) {
tokenize 32 50
}
var %randString
var %randStringLength = $rand(1, $1)
var %i = 1
while (%i <= %randStringLength) {
%randString = %randString $+ $rand(a, z)
if ($rand(1, 4) == 1) {
%randString = %randString $chr(32)
}
inc %i
}
return %randString
}
这篇关于生成随机字符串的脚本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!