Random integer

22 views
Skip to first unread message

Nick Davis

unread,
Jun 26, 2014, 5:37:26 AM6/26/14
to swi-p...@googlegroups.com
Hi all,

I'm quite desperate because I fail to understand why random(+IntExpr) won't generate a single integer but only float numbers between 0 and 1. I'm using the standard SWI-Prolog implementation and can't see why it won't generate an integer.

My aim is to randomly select an element from a list by using nth0, so I definitely need an integer.

Help would be much appreciated.

Yours,

Nick
 

Nick Davis

unread,
Jun 26, 2014, 5:44:04 AM6/26/14
to swi-p...@googlegroups.com
Hi,

I'm sorry for bothering you, I've actually found a solution myself: Simply use random_between_3.

m00nlight

unread,
Jun 26, 2014, 5:46:57 AM6/26/14
to Nick Davis, swi-p...@googlegroups.com
Hi Davis,

You could use random_member(+Elem, +List) to get an random element of a list.
You can find more swi-prolog random related predicates at

http://www.swi-prolog.org/pldoc/man?section=random


--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.
Visit this group at http://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.



--
--m00nlight

Jan Wielemaker

unread,
Jul 14, 2014, 4:37:00 PM7/14/14
to swi-p...@googlegroups.com


On Thursday, June 26, 2014 11:37:26 AM UTC+2, Nick Davis wrote:
Hi all,

I'm quite desperate because I fail to understand why random(+IntExpr) won't generate a single integer but only float numbers between 0 and 1. I'm using the standard SWI-Prolog implementation and can't see why it won't generate an integer.

Because it is an _arithmetic function_.  So, you need

    ?- A is random(6).
    A = 2.

You see this at http://www.swi-prolog.org/pldoc/doc_for?object=f%28random/1%29 because it says
"Availability:Arithmetic function (see is/2)"

There is also a _predicate_ random/1 from library(random), which indeed returns a float.  See
http://www.swi-prolog.org/pldoc/doc_for?object=random/1

      Cheers --- Jan
 
Reply all
Reply to author
Forward
0 new messages