Random number generator
Author: Daniel Diaz
Keywords: GNU Prolog, manual, Prolog, compiler, constraints, finite domains

### 8.25 Random number generator

- set_seed/1, randomize/0
- get_seed/1
- random/1
- random/3

#### 8.25.1 set_seed/1, randomize/0

Templates

  set_seed(+integer)
  randomize

Description

set_seed(Seed) reinitializes the random number generator seed with Seed.

randomize reinitializes the random number generator. This predicates calls set_seed/1 with a random value depending on the absolute time.

Errors

Seed is a variable |  | instantiation_error
Seed is neither a variable nor an integer |  | type_error(integer, Seed)
Seed is an integer < 0 |  | domain_error(not_less_than_zero, Seed)

Portability

GNU Prolog predicates.

#### 8.25.2 get_seed/1

Templates

  get_seed(?integer)

Description

get_seed(Seed) unifies Seed with the current random number generator seed.

Errors

Seed is neither a variable nor an integer |  | type_error(integer, Seed)
Seed is an integer < 0 |  | domain_error(not_less_than_zero, Seed)

Portability

GNU Prolog predicate.

#### 8.25.3 random/1

Templates

  random(-float)

Description

random(Number) unifies Number with a random floating point number such that 0.0 ≤ Number < 1.0.

Errors

Number is not a variable |  | uninstantiation_error(Number)

Portability

GNU Prolog predicate.

#### 8.25.4 random/3

Templates

  random(+number, +number, -number)

Description

random(Base, Max, Number) unifies Number with a random number such that Base ≤ Number < Max. If both Base and Max are integers Number will be an integer, otherwise Number will be a floating point number.

Errors

Base is a variable |  | instantiation_error
Base is neither a variable nor a number |  | type_error(number, Base)
Max is a variable |  | instantiation_error
Max is neither a variable nor a number |  | type_error(number, Max)
Number is not a variable |  | uninstantiation_error(Number)

Portability

GNU Prolog predicate.

Copyright (C) 1999-2018 Daniel Diaz Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. More about the copyright