Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 572717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:38:52+00:00 2026-05-13T13:38:52+00:00

Ok! last Prolog question for a long time!! I’m trying to pick a response

  • 0

Ok! last Prolog question for a long time!!

I’m trying to pick a response that is picked at random but all I can seem to do is pick the first one out of my tables of responses (see code)

I’m sure it’s done with Prologs “findall” and “random” but how?

pick_response(Sent, R) :-
    response(Sent, R), !.
pick_response(_,R) :-
    punt(R),!.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:38:52+00:00Added an answer on May 13, 2026 at 1:38 pm

    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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first StackOverflow question. I'm writing a predicate in prolog that takes three parameters. These
Last few days I searched all over the internet to solve this problem but
I'm trying to write a prolog program that behaves in the following fashion: it
Last year, my group developed a web service that included basic search functionality. All
Last few days I'm trying to start with farseer library, however i just can't
Last year, Scott Guthrie stated You can actually override the raw SQL that LINQ
This is another prolog task that I can't solve at this moment. I have
Last week I created an e-shop with opencart. Now I'm trying to customize the
Last night I tried to put together something that I have had working since
Last night I think I did something that hosed my rails development environment, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.