File: random.3

package info (click to toggle)
erlang-manpages 1%3A12.b.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,188 kB
  • ctags: 2
  • sloc: makefile: 68; perl: 30; sh: 15
file content (108 lines) | stat: -rw-r--r-- 2,621 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
.TH random 3 "stdlib  1.15.3" "Ericsson AB" "ERLANG MODULE DEFINITION"
.SH MODULE
random \- Pseudo random number generation
.SH DESCRIPTION
.LP
Random number generator\&. The method is attributed to B\&.A\&. Wichmann and I\&.D\&.Hill, in \&'An efficient and portable pseudo-random number generator\&', Journal of Applied Statistics\&. AS183\&. 1982\&. Also Byte March 1987\&. 
.LP
The current algorithm is a modification of the version attributed to Richard A O\&'Keefe in the standard Prolog library\&.
.LP
Every time a random number is requested, a state is used to calculate it, and a new state produced\&. The state can either be implicit (kept in the process dictionary) or be an explicit argument and return value\&. In this implementation, the state (the type \fIran()\fR) consists of a tuple of three integers\&.

.SH EXPORTS
.LP
.B
seed() -> ran()
.br
.RS
.LP
Seeds random number generation with default (fixed) values in the process dictionary, and returns the old state\&.
.RE
.LP
.B
seed(A1, A2, A3) -> ran()
.br
.RS
.TP
Types
A1 = A2 = A3 = int()
.br
.RE
.RS
.LP
Seeds random number generation with integer values in the process dictionary, and returns the old state\&.
.LP
One way of obtaining a seed is to use the BIF \fInow/0\fR:

.nf
          \&.\&.\&.
          {A1,A2,A3} = now(),
          random:seed(A1, A2, A3),
          \&.\&.\&.
.fi
.RE
.LP
.B
seed0() -> ran()
.br
.RS
.LP
Returns the default state\&.
.RE
.LP
.B
uniform()-> float()
.br
.RS
.LP
Returns a random float uniformly distributed between \fI0\&.0\fR and \fI1\&.0\fR, updating the state in the process dictionary\&.
.RE
.LP
.B
uniform(N) -> int()
.br
.RS
.TP
Types
N = int()
.br
.RE
.RS
.LP
Given an integer \fIN >= 1\fR, \fIuniform/1\fR returns a random integer uniformly distributed between \fI1\fR and \fIN\fR, updating the state in the process dictionary\&.
.RE
.LP
.B
uniform_s(State0) -> {float(), State1}
.br
.RS
.TP
Types
State0 = State1 = ran()
.br
.RE
.RS
.LP
Given a state, \fIuniform_s/1\fRreturns a random float uniformly distributed between \fI0\&.0\fR and \fI1\&.0\fR, and a new state\&.
.RE
.LP
.B
uniform_s(N, State0) -> {int(), State1}
.br
.RS
.TP
Types
N = int()
.br
State0 = State1 = ran()
.br
.RE
.RS
.LP
Given an integer \fIN >= 1\fR and a state, \fIuniform_s/2\fR returns a random integer uniformly distributed between \fI1\fR and \fIN\fR, and a new state\&.
.RE
.SH NOTE
.LP
Some of the functions use the process dictionary variable \fIrandom_seed\fR to remember the current seed\&.
.LP
If a process calls \fIuniform/0\fR or \fIuniform/1\fR without setting a seed first, \fIseed/0\fR is called automatically\&.