-
Notifications
You must be signed in to change notification settings - Fork 106
/
batRandom.ml
143 lines (112 loc) · 3.79 KB
/
batRandom.ml
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(*
* ExtRandom - Additional randomization operations
* Copyright (C) 1996 Damien Doligez
* 2009 David Teller, LIFO, Universite d'Orleans
* 2009 Pierre Chambart
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
open Random
let init = init
let full_init = full_init
let self_init = self_init
let bits = bits
let int = int
let int32 = int32
let int64 = int64
let nativeint = nativeint
let float = float
let bool = bool
let char () = Char.chr (int 256)
module State =
struct
include State (*Note: here, we use [Marshal] to avoid breaking abstraction. So it's not portable.*)
let char t = Char.chr (int t 256)
(**A constructor for enumerations of random numbers taking advantage
of [State] to allow cloning.*)
let random_enum state next =
let rec aux state =
let next () = next state in
let count () = raise BatEnum.Infinite_enum in
let clone () = aux ( State.copy state ) in
BatEnum.make next count clone
in aux state
let enum_bits state () =
let next state = bits state in
random_enum state next
let enum_int state bound =
let next state = int state bound in
random_enum state next
let enum_int32 state bound =
let next state = int32 state bound in
random_enum state next
let enum_int64 state bound =
let next state = int64 state bound in
random_enum state next
let enum_float state bound =
let next state = float state bound in
random_enum state next
let enum_nativeint state bound =
let next state = nativeint state bound in
random_enum state next
let enum_bool state () =
let next state = bool state in
random_enum state next
let enum_char state () =
let next state = char state in
random_enum state next
end
let random_enum next = State.random_enum ( State.make_self_init () ) next
let enum_bits () =
let next state = State.bits state in
random_enum next
let enum_int bound =
let next state = State.int state bound in
random_enum next
let enum_int32 bound =
let next state = State.int32 state bound in
random_enum next
let enum_int64 bound =
let next state = State.int64 state bound in
random_enum next
let enum_float bound =
let next state = State.float state bound in
random_enum next
let enum_nativeint bound =
let next state = State.nativeint state bound in
random_enum next
let enum_bool () =
let next state = State.bool state in
random_enum next
let enum_char () =
let next state = State.char state in
random_enum next
let choice e =
let a = BatArray.of_enum e in
let len = Array.length a in
Array.get a (int len)
let shuffle e =
let a = BatArray.of_enum e in
for n = Array.length a - 1 downto 1 do
let k = int ( n + 1 ) in
if k <> n then
let buf = Array.get a n in
Array.set a n (Array.get a k);
Array.set a k buf
done;
a
let get_state = get_state
let set_state = set_state