Random member of a list
Author Message
Random member of a list

Quote:

> you generate a random number between 1 and length(List) where List is
> your List of words
> then u could use nth1(Generatednumber,List,Randomword)

> nth1(?Index, ?List, ?Elem)
>     Succeeds  when  the  Index-th element  of  List unifies  with
> Elem.
>     Counting starts at 1.

> nth1/3 is predefined in SWI-Prolog

Hmmm. Since random/1 in SWI generates 0 to N-1, nth0/3 appears
to be the better choice.

--
(`'`'`'`)       Geoffrey Summerhayes
|     |   ------------------------------
|    ``   (defun mail-to()
(c---()()   (apply #'concatenate 'string
|    __)    (mapcar #'reverse

/_____/#\       "liamtoh" "." "moc" ))))

Tue, 01 Jun 2004 05:44:42 GMT
Random member of a list
How can I select a random member of a list?

What I'm trying to do is to write a little program that combines one
random word from a list with another random word from another list. I've
tried searching for advice on the net but all I can find is explanations
of random number generation.

This is not my homework, it's just something I do for fun, to try to
learn Prolog a little better. I only know very basic stuff so far.
Thanks in advance for explaining! :)

This is my tiny little code so far:
---------------------------
generate_term:-
random_first(Term1),
random_second(Term2),
write(Term1),
write(Term2),
nl.

random_first(Term1):-
(what here?)

random_second(Term2):-
(what here?)

first_terms([word1, word2, word3]).
second_terms([word4, word5, word6]).
--------------------------

--

I offer you my body, and you give me semantics?

Tue, 01 Jun 2004 03:01:21 GMT
Random member of a list

Hi,

Quote:
> How can I select a random member of a list?
> What I'm trying to do is to write a little program that combines one
> random word from a list with another random word from another list. I've
> tried searching for advice on the net but all I can find is explanations
> of random number generation.

well then it "should" be easy ..., but i dunno if it works in other
versions than SWI-Prolog, i'm not an expert :)

you generate a random number between 1 and length(List) where List is
your List of words
then u could use nth1(Generatednumber,List,Randomword)

nth1(?Index, ?List, ?Elem)
Succeeds  when  the  Index-th element  of  List unifies  with
Elem.
Counting starts at 1.

nth1/3 is predefined in SWI-Prolog

HTH
Christoph

Tue, 01 Jun 2004 04:15:10 GMT
Random member of a list
%
% my version .... for GNU Prolog
%

generate_term:-
random_list([word1, word2, word3],Term1),
random_list([word4, word5, word6],Term2),
write(Term1),
write(Term2),
nl.

%
% random_list(List,Term) <=> Term is a term randomly found in List
%

random_list(List,Term):-
length(List,MaxIndice),
MaxRandom is MaxIndice+1,
random(1,MaxRandom,NbElement),  % a GNU Prolog predicate : not ISO
prolog
element_of_list(List,NbElement,Term).

%
% element(L,I,X)  <=> X is the Ith element of the list L
%

element_of_list([],_,_):-!,fail.
element_of_list([X|L],1,X):-!.
element_of_list([_|L],I,X):-I > 1,I2 is I - 1,element_of_list(L,I2,X).

%
% Jean Michel LECONTE
%

Quote:

> This is my tiny little code so far:
> ---------------------------
> generate_term:-
>  random_first(Term1),
>  random_second(Term2),
>  write(Term1),
>  write(Term2),
>  nl.

> random_first(Term1):-
>  (what here?)

> random_second(Term2):-
>  (what here?)

> first_terms([word1, word2, word3]).
> second_terms([word4, word5, word6]).
> --------------------------

> --

> I offer you my body, and you give me semantics?

Tue, 01 Jun 2004 04:24:49 GMT
Random member of a list
Thanks a lot for your help, this looks very right - but I can't get it
to work :(
I'm posting the code and the error messages - if anyone has an idea what
might be causing this, please let me know.. :)

I'm using GNU Prolog (version 1.2.1), so incompatibility shouldn't be a
problem. During my search for errors I stripped the test file so only
the code itself is left. There are as many errors as there are
predicates - it seems like the "end dot" somehow isn't "acknowledged" -
I even rewrote the whole thing manually into another file for testing -
same error. Is it just my system that is messed up?

code:
----------
generate:-
randomlist([semantic,selectional,internal,grammar,cognition,primitive,co
mponential,contextual,lexical,thematic,information,feature,bootstrapping
,subject,object,binary,relevance,query],Term1),
randomlist([theory,roles,agent,grammar,restrictions,interpretations,hier
archy,alternation,violation,
database,decomposition,analysis,field,disambiguation,processing,retrieva
l,model,strategy,limitations,approaches,classifier,clustering,dictionari
es,normalizing,frequency,relevance,stemming,precision,recall,expansion],
Term2),
write(Term1),
write(Term2),
nl.

randomlist(List,Term):-
length(List,MaxIndice),
MaxRandom is MaxIndice+1,
random(1,MaxRandom,NbElement),
elementoflist(List,NbElement,Term).

elementoflist([],_,_):-!,fail.
elementoflist([X|L],1,X):-!.
elementoflist([_|L],I,X):-I > 1,I2 is I - 1,elementoflist(L,I2,X).
----------------

errors:

| ?- consult('generator').
compiling /export/home/eira/generator.pl for byte code...
/export/home/eira/generator.pl:1 error: syntax error: . or operator
expected after expression (char:9)
/export/home/eira/generator.pl:8 error: syntax error: . or operator
expected after expression (char:22)
/export/home/eira/generator.pl:14 error: syntax error: . or operator
expected after expression (char:22)
/export/home/eira/generator.pl:15 error: syntax error: . or operator
expected after expression (char:25)
/export/home/eira/generator.pl:16 error: syntax error: . or operator
expected after expression (char:25)
5 error(s)
compilation failed

no

Tue, 01 Jun 2004 06:42:07 GMT
Random member of a list
em a crit :

Quote:

> Thanks a lot for your help, this looks very right - but I can't get it
> to work :(
> I'm posting the code and the error messages - if anyone has an idea what
> might be causing this, please let me know.. :)

> I'm using GNU Prolog (version 1.2.1), so incompatibility shouldn't be a
> problem. During my search for errors I stripped the test file so only
> the code itself is left. There are as many errors as there are
> predicates - it seems like the "end dot" somehow isn't "acknowledged" -
> I even rewrote the whole thing manually into another file for testing -
> same error. Is it just my system that is messed up?

> code:
> ----------
> generate:-
> randomlist([semantic,selectional,internal,grammar,cognition,primitive,co
> mponential,contextual,lexical,thematic,information,feature,bootstrapping
> ,subject,object,binary,relevance,query],Term1),
> randomlist([theory,roles,agent,grammar,restrictions,interpretations,hier
> archy,alternation,violation,
> database,decomposition,analysis,field,disambiguation,processing,retrieva
> l,model,strategy,limitations,approaches,classifier,clustering,dictionari
> es,normalizing,frequency,relevance,stemming,precision,recall,expansion],
> Term2),
> write(Term1),
> write(Term2),
> nl.

> randomlist(List,Term):-
> length(List,MaxIndice),
> MaxRandom is MaxIndice+1,
> random(1,MaxRandom,NbElement),
> elementoflist(List,NbElement,Term).

> elementoflist([],_,_):-!,fail.
> elementoflist([X|L],1,X):-!.
> elementoflist([_|L],I,X):-I > 1,I2 is I - 1,elementoflist(L,I2,X).

What does it do when you write all of each call like
randomlist([semantic,selectional,internal,grammar,cognition,primitive,componential,contextual,lexical,thematic,information,feature,boottrapping,subject,object,binary,relevance,query],Term1)

on only one line ???

Because tht's working on GNU prolog 1.2.8

Tue, 01 Jun 2004 07:26:59 GMT
Random member of a list

Quote:
> em a crit :

> (...)

> What does it do when you write all of each call like

randomlist([semantic,selectional,internal,grammar,cognition,primitive,co
mponential,contextual,lexical,thematic,information,feature,boottrapping,
subject,object,binary,relevance,query],Term1)

Quote:

> on only one line ???

> Because tht's working on GNU prolog 1.2.8

That's what my code is like when i try to consult it - no line breaks in
the call. My news client breaks the lines. I also tried the file you
emailed, with the exact same error messages. I suppose the problem lies
in my system then - I'll take it from there. Thanks anyway, I can learn
from the code even though I can't run it :)

- Eira

Tue, 01 Jun 2004 07:37:09 GMT
Random member of a list

Quote:

> compiling /export/home/eira/generator.pl for byte code...
> /export/home/eira/generator.pl:1 error: syntax error: . or operator
> expected after expression (char:9)

Character 9 on line 1 seems to be the start of the :- operator.  Perhaps
your system is messed up (lacking operator definitions)?

--

http://www.bawue.de/~jjk/          fax:+49-7031-464-7351
PGP:       06 04 1C 35 7B DC 1F 26 As the air to a bird, or the sea to a fish,
0x555DA8B5 BB A2 F0 66 77 75 E1 08 so is contempt to the contemptible. [Blake]

Tue, 01 Jun 2004 16:13:44 GMT
Random member of a list
Maybe the tokenizer was buggy in that version, so it needs a space
before the ":-"?

--
Daniel W. Brown (remove xxx to reply)
Computer Scientist, SRI International

Tue, 01 Jun 2004 22:01:48 GMT
Random member of a list

Quote:

in

> > em a crit :

> I suppose the problem lies
> in my system then - I'll take it from there.

Problem solved - sysadmin rebooted server, program working fine now :)

Wed, 02 Jun 2004 00:48:40 GMT

 Page 1 of 1 [ 10 post ]

Relevant Pages