1Random(3) OCaml library Random(3)
2
3
4
6 Random - Pseudo-random number generators (PRNG).
7
9 Module Random
10
12 Module Random
13 : sig end
14
15
16 Pseudo-random number generators (PRNG).
17
18
19
20
21
22
23
24 Basic functions
25 val init : int -> unit
26
27 Initialize the generator, using the argument as a seed. The same seed
28 will always yield the same sequence of numbers.
29
30
31
32 val full_init : int array -> unit
33
34 Same as Random.init but takes more data as seed.
35
36
37
38 val self_init : unit -> unit
39
40 Initialize the generator with a random seed chosen in a system-depen‐
41 dent way. If /dev/urandom is available on the host machine, it is used
42 to provide a highly random initial seed. Otherwise, a less random seed
43 is computed from system parameters (current time, process IDs).
44
45
46
47 val bits : unit -> int
48
49 Return 30 random bits in a nonnegative integer.
50
51
52 Before3.12.0 used a different algorithm (affects all the following
53 functions)
54
55
56
57
58 val int : int -> int
59
60
61 Random.int bound returns a random integer between 0 (inclusive) and
62 bound (exclusive). bound must be greater than 0 and less than 2^30.
63
64
65
66 val full_int : int -> int
67
68
69 Random.full_int bound returns a random integer between 0 (inclusive)
70 and bound (exclusive). bound may be any positive integer.
71
72 If bound is less than 2^30, Random.full_int bound is equal to Ran‐
73 dom.int bound . If bound is greater than 2^30 (on 64-bit systems or
74 non-standard environments, such as JavaScript), Random.full_int returns
75 a value, where Random.int raises Invalid_argument .
76
77
78 Since 4.13.0
79
80
81
82 val int32 : Int32.t -> Int32.t
83
84
85 Random.int32 bound returns a random integer between 0 (inclusive) and
86 bound (exclusive). bound must be greater than 0.
87
88
89
90 val nativeint : Nativeint.t -> Nativeint.t
91
92
93 Random.nativeint bound returns a random integer between 0 (inclusive)
94 and bound (exclusive). bound must be greater than 0.
95
96
97
98 val int64 : Int64.t -> Int64.t
99
100
101 Random.int64 bound returns a random integer between 0 (inclusive) and
102 bound (exclusive). bound must be greater than 0.
103
104
105
106 val float : float -> float
107
108
109 Random.float bound returns a random floating-point number between 0 and
110 bound (inclusive). If bound is negative, the result is negative or
111 zero. If bound is 0, the result is 0.
112
113
114
115 val bool : unit -> bool
116
117
118 Random.bool () returns true or false with probability 0.5 each.
119
120
121
122 val bits32 : unit -> Int32.t
123
124
125 Random.bits32 () returns 32 random bits as an integer between
126 Int32.min_int and Int32.max_int .
127
128
129 Since 4.14.0
130
131
132
133 val bits64 : unit -> Int64.t
134
135
136 Random.bits64 () returns 64 random bits as an integer between
137 Int64.min_int and Int64.max_int .
138
139
140 Since 4.14.0
141
142
143
144 val nativebits : unit -> Nativeint.t
145
146
147 Random.nativebits () returns 32 or 64 random bits (depending on the bit
148 width of the platform) as an integer between Nativeint.min_int and Na‐
149 tiveint.max_int .
150
151
152 Since 4.14.0
153
154
155
156
157 Advanced functions
158 The functions from module Random.State manipulate the current state of
159 the random generator explicitly. This allows using one or several de‐
160 terministic PRNGs, even in a multi-threaded program, without interfer‐
161 ence from other parts of the program.
162
163 module State : sig end
164
165
166
167
168
169 val get_state : unit -> State.t
170
171 Return the current state of the generator used by the basic functions.
172
173
174
175 val set_state : State.t -> unit
176
177 Set the state of the generator used by the basic functions.
178
179
180
181
182
183OCamldoc 2023-01-23 Random(3)