String Prolog-生成随机字符串
我对Prolog非常陌生,我正在尝试构建一个脚本,在这个脚本中我可以用Prolog生成一个随机字符串。到目前为止,我运气不太好 我这样做的想法是生成所需字符的列表,根据列表的大小创建一个随机数,然后尝试从该列表中提取一个字符。这很好,但我还需要能够连接到一个变量上,这个变量显然一开始什么都没有。现在我只是想把字符串的大小限制在5个字符以内,但将来我会想要一些更复杂的东西(比如5-8个字符) 以下是我到目前为止的情况:String Prolog-生成随机字符串,string,random,prolog,String,Random,Prolog,我对Prolog非常陌生,我正在尝试构建一个脚本,在这个脚本中我可以用Prolog生成一个随机字符串。到目前为止,我运气不太好 我这样做的想法是生成所需字符的列表,根据列表的大小创建一个随机数,然后尝试从该列表中提取一个字符。这很好,但我还需要能够连接到一个变量上,这个变量显然一开始什么都没有。现在我只是想把字符串的大小限制在5个字符以内,但将来我会想要一些更复杂的东西(比如5-8个字符) 以下是我到目前为止的情况: generate :- Output = '', genera
generate :-
Output = '',
generate_random_string(Output, 0),
write(Output).
generate_random_string(Output, 5) :-
Characters = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
random(0, 14, RandomValue),
nth0(RandomValue, Characters, RandomCharacter),
append(RandomCharacter, Output, Concat),
Output = Concat.
generate_random_string(Output, CharNum) :-
CharNum \= 5,
Characters = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
random(0, 14, RandomValue),
nth0(RandomValue, Characters, RandomCharacter),
append(RandomCharacter, Output, Concat),
Count is CharNum + 1,
Output = Concat,
generate_random_string(Output, Count).
我相信这是打破了append调用,但我不确定为什么,尽管我知道它在第一次迭代中就被打破了,它给出了一个false的结果。如有任何帮助,我们将不胜感激。因此,正如评论中所建议的,您应该使用
atom\u concat/3
,方法如下:
generate :-
Output = 'abc',
generate_random_string(Output,Result,0),
write(Result).
generate_random_string(Output,Result,5):- !,
Characters = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
random(0, 14, RandomValue),
nth0(RandomValue, Characters, RandomCharacter),
atom_concat(RandomCharacter,Output,Result).
generate_random_string(Output,Result,CharNum) :-
CharNum \= 5,
Characters = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
random(0, 14, RandomValue),
nth0(RandomValue, Characters, RandomCharacter),
atom_concat(RandomCharacter,Output,Concat),
Count is CharNum + 1,
generate_random_string(Concat,Result,Count).
查询:
?- generate.
feFaaCabc
true
?- random_string(4,'gr',S).
S = grAd % note gr is in the string
?- random_string(4,'',S).
S = fdGb
如您所见,如果您输入一个字符串,并且希望向其添加一些字符,程序将输出一个指定长度的字符串(在您的情况下,5+1
,因为您从0开始)加上输入字符串的长度
使用random\u member/2
,可以采用以下不同的模块化方法:
pick_random(_,0,L,L):- !.
pick_random(LC,I,SIn,SOut):-
I > 0,
random_member(R,LC),
atom_concat(SIn,R,Concat),
I1 is I-1,
pick_random(LC,I1,Concat,SOut).
random_string(Len,SIn,SOut):-
LC = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
string_length(SIn,Ls),
Len1 is Len - Ls,
pick_random(LC,Len1,SIn,SOut).
characters(['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g']).
generate(L,N) :-
length(L,N),
characters(Char_list),
length(Char_list, N1),
maplist( random_char_generat(Char_list, N1), L ).
random_char_generat(Char_list, N, Char):- random(0, N, X), nth0(X, Char_list, Char ).
查询:
?- generate.
feFaaCabc
true
?- random_string(4,'gr',S).
S = grAd % note gr is in the string
?- random_string(4,'',S).
S = fdGb
从这个起点出发,很容易将字符串的长度设置为随机的。因此,正如注释中所建议的,您应该使用
atom\u concat/3
,如下所示:
generate :-
Output = 'abc',
generate_random_string(Output,Result,0),
write(Result).
generate_random_string(Output,Result,5):- !,
Characters = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
random(0, 14, RandomValue),
nth0(RandomValue, Characters, RandomCharacter),
atom_concat(RandomCharacter,Output,Result).
generate_random_string(Output,Result,CharNum) :-
CharNum \= 5,
Characters = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
random(0, 14, RandomValue),
nth0(RandomValue, Characters, RandomCharacter),
atom_concat(RandomCharacter,Output,Concat),
Count is CharNum + 1,
generate_random_string(Concat,Result,Count).
查询:
?- generate.
feFaaCabc
true
?- random_string(4,'gr',S).
S = grAd % note gr is in the string
?- random_string(4,'',S).
S = fdGb
如您所见,如果您输入一个字符串,并且希望向其添加一些字符,程序将输出一个指定长度的字符串(在您的情况下,5+1
,因为您从0开始)加上输入字符串的长度
使用random\u member/2
,可以采用以下不同的模块化方法:
pick_random(_,0,L,L):- !.
pick_random(LC,I,SIn,SOut):-
I > 0,
random_member(R,LC),
atom_concat(SIn,R,Concat),
I1 is I-1,
pick_random(LC,I1,Concat,SOut).
random_string(Len,SIn,SOut):-
LC = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
string_length(SIn,Ls),
Len1 is Len - Ls,
pick_random(LC,Len1,SIn,SOut).
characters(['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g']).
generate(L,N) :-
length(L,N),
characters(Char_list),
length(Char_list, N1),
maplist( random_char_generat(Char_list, N1), L ).
random_char_generat(Char_list, N, Char):- random(0, N, X), nth0(X, Char_list, Char ).
查询:
?- generate.
feFaaCabc
true
?- random_string(4,'gr',S).
S = grAd % note gr is in the string
?- random_string(4,'',S).
S = fdGb
从这一点出发,很容易使字符串的长度随机。这里有一个更简单的解决方案:
characters(['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g']).
generate(L,N) :-
length(L,N),
characters(Char_list),
N1 is N-1,
foreach( (between(0,N1,X), random(0, 14, RandomValue), nth0(RandomValue, Char_list, Char)) , nth0(X,L,Char) ).
请注意,上述解决方案将数字N作为参数,并生成N位随机字符列表:
?- generate(L,5).
L = [f, e, 'C', 'B', 'A'].
?- generate(L,8).
L = [a, 'E', c, c, 'E', f, 'B', g].
?- generate(L,N).
L = [],
N = 0 ;
L = ['B'],
N = 1 ;
L = [c, 'F'],
N = 2 ;
L = ['E', g, 'C'],
N = 3 ;
L = ['C', c, 'F', 'D'],
N = 4 ;
L = [c, 'B', 'F', 'B', 'B'],
N = 5
....and goes on
下面是一个更简单的解决方案:
characters(['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g']).
generate(L,N) :-
length(L,N),
characters(Char_list),
N1 is N-1,
foreach( (between(0,N1,X), random(0, 14, RandomValue), nth0(RandomValue, Char_list, Char)) , nth0(X,L,Char) ).
请注意,上述解决方案将数字N作为参数,并生成N位随机字符列表:
?- generate(L,5).
L = [f, e, 'C', 'B', 'A'].
?- generate(L,8).
L = [a, 'E', c, c, 'E', f, 'B', g].
?- generate(L,N).
L = [],
N = 0 ;
L = ['B'],
N = 1 ;
L = [c, 'F'],
N = 2 ;
L = ['E', g, 'C'],
N = 3 ;
L = ['C', c, 'F', 'D'],
N = 4 ;
L = [c, 'B', 'F', 'B', 'B'],
N = 5
....and goes on
由于@PauloMoura在前面的回答中评论说,
foreach/3
可能不可移植,因为它不是ISO谓词,因此这里有一个更好的解决方案,使用maplist/2
:
pick_random(_,0,L,L):- !.
pick_random(LC,I,SIn,SOut):-
I > 0,
random_member(R,LC),
atom_concat(SIn,R,Concat),
I1 is I-1,
pick_random(LC,I1,Concat,SOut).
random_string(Len,SIn,SOut):-
LC = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
string_length(SIn,Ls),
Len1 is Len - Ls,
pick_random(LC,Len1,SIn,SOut).
characters(['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g']).
generate(L,N) :-
length(L,N),
characters(Char_list),
length(Char_list, N1),
maplist( random_char_generat(Char_list, N1), L ).
random_char_generat(Char_list, N, Char):- random(0, N, X), nth0(X, Char_list, Char ).
上述解决方案在下列条件下更好:
- 它适用于生成的任意长度的随机字符列表
- 它适用于任何长度的可用字符列表
- 最后,正如您在下面的示例中所看到的,它是纯的,适用于非常一般的查询:
由于@PauloMoura在前面的回答中评论说,
foreach/3
可能不可移植,因为它不是ISO谓词,因此这里有一个更好的解决方案,使用maplist/2
:
pick_random(_,0,L,L):- !.
pick_random(LC,I,SIn,SOut):-
I > 0,
random_member(R,LC),
atom_concat(SIn,R,Concat),
I1 is I-1,
pick_random(LC,I1,Concat,SOut).
random_string(Len,SIn,SOut):-
LC = ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g'],
string_length(SIn,Ls),
Len1 is Len - Ls,
pick_random(LC,Len1,SIn,SOut).
characters(['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g']).
generate(L,N) :-
length(L,N),
characters(Char_list),
length(Char_list, N1),
maplist( random_char_generat(Char_list, N1), L ).
random_char_generat(Char_list, N, Char):- random(0, N, X), nth0(X, Char_list, Char ).
上述解决方案在下列条件下更好:
- 它适用于生成的任意长度的随机字符列表
- 它适用于任何长度的可用字符列表
- 最后,正如您在下面的示例中所看到的,它是纯的,适用于非常一般的查询:
查看
append/3
的文档<代码>附加/3对列表进行操作。您可能需要atom\u concat/3
。请查看append/3
的文档<代码>附加/3对列表进行操作。您可能需要atom\u concat/3。非常棒的东西,谢谢。我已经到了,但这是有道理的。第二部分要好得多,但我自己不会写,所以我会努力理解它。很棒的东西,谢谢。我已经到了,但这是有道理的。第二部分要好得多,但我自己不会写,所以我会尽力理解它。请注意,foreach/2
谓词不是ISO或事实上的标准谓词,这严重限制了此解决方案的可移植性。@PauloMoura,谢谢您的评论!!检查我的另一个答案,它解决了您提到的问题。maplist/2
和length/2
谓词确实更通用。请注意,foreach/2
谓词既不是ISO也不是事实上的标准谓词,这严重限制了此解决方案的可移植性。@PauloMoura,谢谢您的评论!!检查我的另一个答案,它解决了您提到的问题。maplist/2
和length/2
谓词确实更通用。