One way to do it with findall/3
and random/3
is:
% Responses for sentence 'sentence'
response(sentence, first).
response(sentence, second).
response(sentence, third).
% 1. Generate a list of all responses
% 2. Generate a random integer
% 3. Pick the response with the index of the integer from the list
random_response(Sentence, RandomResponse) :-
findall(Response, response(Sentence, Response), List),
length(List, Len),
random(0, Len, RandomNumber),
nth0(RandomNumber, List, RandomResponse).
Usage:
?- random_response(sentence, RandomResponse).
RandomResponse = third.
?- random_response(sentence, RandomResponse).
RandomResponse = first.
?- random_response(sentence, RandomResponse).
RandomResponse = second.
?- random_response(sentence, RandomResponse).
RandomResponse = second.
?- random_response(sentence, RandomResponse).
RandomResponse = second.
?- random_response(sentence, RandomResponse).
RandomResponse = third.