# Random integer

22 views

### Nick Davis

Jun 26, 2014, 5:37:26 AM6/26/14
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

Jun 26, 2014, 5:44:04 AM6/26/14
Hi,

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

### m00nlight

Jun 26, 2014, 5:46:57 AM6/26/14
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.

--
--m00nlight

### Jan Wielemaker

Jul 14, 2014, 4:37:00 PM7/14/14

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