Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random items in Prolog

I know I can do X is random(10). to get a random number from 0 to 10, but is there a similar command to get a random matching item?

like image 951
Pieter Avatar asked Feb 14 '10 13:02

Pieter


1 Answers

You can implement it. Here is a version:

%% choose(List, Elt) - chooses a random element
%% in List and unifies it with Elt.
choose([], []).
choose(List, Elt) :-
        length(List, Length),
        random(0, Length, Index),
        nth0(Index, List, Elt).

From http://ozone.wordpress.com/2006/02/22/little-prolog-challenge/

like image 153
Juanjo Conti Avatar answered Nov 15 '22 11:11

Juanjo Conti