~ubuntu-branches/ubuntu/precise/ocaml-batteries/precise

« back to all changes in this revision

Viewing changes to src/batRandom.ml

  • Committer: Bazaar Package Importer
  • Author(s): Stefano Zacchiroli
  • Date: 2010-03-06 16:03:38 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100306160338-spvwiv3uc4jr28hw
Tags: 1.1.0-1
* New upstream release
  - major changes, "diet" version of the library
  - fix old FTBFS error, due to major code changes (Closes: #569455)
* Revamp packaging
  - adapt to new stuff shipped by upstream
  - switch from CDBS to dh
  - adapt dependencies (generally: reduce them)
* debian/patches/
  - remove old debian/patches/{debian-specific-installation-paths,
    debian-specific-info-on-doc-availability} as obsolete
  - new patch 0001-install-fix-for-bytecode-only-build: avoid
    installing *.a files with bytecode only compilation
* debian/libbatteries-ocaml-dev.links: remove file, shortend
  /usr/bin/ocaml-batteries to the top-level no longer exists
* remove debian/README.Debian (previous content is now obsolete)
* bump Standards-Version to 3.8.4 (no changes needed)
* debian/watch: update to match new upstream version convention
* debian/libbatteries-ocaml-{dev,doc}.{docs,examples}: ship only doc
  file from the root dir, other stuff is currently out of date
  (Closes: #514265)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(* 
 
2
 * ExtRandom - Additional randomization operations
 
3
 * Copyright (C) 1996 Damien Doligez
 
4
 *               2009 David Teller, LIFO, Universite d'Orleans
 
5
 *               2009 Pierre Chambart
 
6
 * 
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version,
 
11
 * with the special exception on linking described in file LICENSE.
 
12
 *
 
13
 * This library is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this library; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 *)
 
22
 
 
23
 
 
24
  open Random
 
25
 
 
26
  let init      = init
 
27
  let full_init = full_init
 
28
  let self_init = self_init
 
29
  let bits      = bits
 
30
  let int       = int
 
31
  let int32     = int32
 
32
  let int64     = int64
 
33
  let nativeint = nativeint
 
34
  let float     = float
 
35
  let bool      = bool
 
36
 
 
37
  let char ()   = Char.chr (int 256)
 
38
 
 
39
 
 
40
  module State =
 
41
  struct
 
42
    include State (*Note: here, we use [Marshal] to avoid breaking abstraction. So it's not portable.*)
 
43
 
 
44
    let char t   = Char.chr (int t 256)
 
45
 
 
46
    (**A constructor for enumerations of random numbers taking advantage
 
47
       of [State] to allow cloning.*)
 
48
    let random_enum state next =
 
49
      let rec aux state =
 
50
        let next  () = next state in
 
51
        let count () = raise BatEnum.Infinite_enum in
 
52
        let clone () = aux ( State.copy state ) in
 
53
          BatEnum.make next count clone
 
54
      in aux state
 
55
 
 
56
    let enum_bits state () =
 
57
      let next state = bits state in
 
58
        random_enum state next
 
59
 
 
60
    let enum_int state bound =
 
61
      let next state = int state bound in
 
62
        random_enum state next
 
63
 
 
64
    let enum_int32 state bound =
 
65
      let next state = int32 state bound in
 
66
        random_enum state next
 
67
 
 
68
    let enum_int64 state bound =
 
69
      let next state = int64 state bound in
 
70
        random_enum state next
 
71
 
 
72
    let enum_float state bound =
 
73
      let next state = float state bound in
 
74
        random_enum state next
 
75
 
 
76
    let enum_nativeint state bound =
 
77
      let next state = nativeint state bound in
 
78
        random_enum state next
 
79
 
 
80
    let enum_bool state () =
 
81
      let next state = bool state in
 
82
        random_enum state next
 
83
 
 
84
    let enum_char state () =
 
85
      let next state = char state in
 
86
        random_enum state next
 
87
 
 
88
  end
 
89
 
 
90
  let random_enum next = State.random_enum ( State.make_self_init () ) next
 
91
 
 
92
  let enum_bits () =
 
93
    let next state = State.bits state in
 
94
      random_enum next
 
95
 
 
96
  let enum_int bound =
 
97
    let next state = State.int state bound in
 
98
      random_enum next
 
99
 
 
100
  let enum_int32 bound =
 
101
    let next state = State.int32 state bound in
 
102
      random_enum next
 
103
        
 
104
  let enum_int64 bound =
 
105
    let next state = State.int64 state bound in
 
106
      random_enum next
 
107
        
 
108
  let enum_float bound =
 
109
    let next state = State.float state bound in
 
110
      random_enum next
 
111
        
 
112
  let enum_nativeint bound =
 
113
    let next state = State.nativeint state bound in
 
114
      random_enum next
 
115
        
 
116
  let enum_bool () =
 
117
    let next state = State.bool state in
 
118
      random_enum next
 
119
 
 
120
  let enum_char () =
 
121
    let next state = State.char state in
 
122
      random_enum next
 
123
 
 
124
  let choice e =
 
125
    let a   = BatArray.of_enum e in
 
126
    let len = Array.length  a in
 
127
      Array.get a (int len)
 
128
 
 
129
  let shuffle e =
 
130
    let a = BatArray.of_enum e in
 
131
      for n = Array.length a - 1 downto 1 do
 
132
        let k    = int ( n + 1 ) in
 
133
          if k <> n then
 
134
            let buf  = Array.get a n in
 
135
              Array.set a n (Array.get a k);
 
136
              Array.set a k buf
 
137
      done;
 
138
      a
 
139
 
 
140
  let get_state = get_state
 
141
  let set_state = set_state
 
142
 
 
143