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?
views:
282answers:
1
A:
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/
Juanjo Conti
2010-02-15 12:54:07
Whoa there, the first clause of choose/2 isn't what you want. If the list it empty you should produce no solutions instead of unifying Elt with the empty list. "choose([],_) :- !, fail." would fix it.Also, this generates an infinite number of solutions which is usually undesirable.
rndmcnlly
2010-03-01 04:17:42