/usr/lib/ats-anairiats-0.2.11/libc/DATS/random.dats is in ats-lang-anairiats 0.2.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | (***********************************************************************)
(* *)
(* Applied Type System *)
(* *)
(* Hongwei Xi *)
(* *)
(***********************************************************************)
(*
** ATS - Unleashing the Potential of Types!
**
** Copyright (C) 2002-2010 Hongwei Xi, Boston University
**
** All rights reserved
**
** ATS is free software; you can redistribute it and/or modify it under
** the terms of the GNU General Public License as published by the Free
** Software Foundation; either version 2.1, or (at your option) any later
** version.
**
** ATS 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 General Public License
** for more details.
**
** You should have received a copy of the GNU General Public License
** along with ATS; see the file COPYING. If not, please write to the
** Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*)
(* ****** ****** *)
(* author: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *)
(* ****** ****** *)
#define ATS_DYNLOADFLAG 0 // no dynamic loading
(* ****** ****** *)
staload _(*anon*) = "prelude/DATS/array.dats"
(* ****** ****** *)
staload "libc/sys/SATS/time.sats" // for gettimeofday
staload "libc/sys/SATS/types.sats" // for several lint_of* functions
(* ****** ****** *)
staload "libc/SATS/random.sats"
(* ****** ****** *)
implement
srand48_with_gettimeofday () = let
var tv: timeval?
val err = gettimeofday (tv)
val () = if err = 0 then let
prval () = opt_unsome {timeval} (tv)
val seed = (lint_of)tv.tv_sec * 1000000L + (lint_of)tv.tv_usec
val () = srand48 (seed)
in
// nothing
end else let
prval () = opt_unnone {timeval} (tv)
in
// nothing
end // end of [if]
in
err (* 0/-1 : succ/fail *)
end // end of [srand48_with_gettimeofday]
(* ****** ****** *)
implement
randint {n} (n) = let
val d01 = drand48 ()
val r = int_of (d01 * n)
val [r:int] r = int1_of (r)
prval () = __assert () where {
extern prfun __assert (): [0 <= r; r <= n] void
} // end of [prval]
in
if r < n then r else 0
end // end of [randint]
local
staload UN = "prelude/SATS/unsafe.sats"
in // in of [local]
implement
randsize {n} (n) = let
val d01 = drand48 ()
val r = ullint_of_double (d01 * (double_of_size)n)
val r = $UN.cast2size (r)
val [r:int] r = size1_of_size (r)
prval () = __assert () where {
extern prfun __assert (): [0 <= r; r <= n] void
} // end of [prval]
in
if r < n then r else (size1_of_int1)0
end // end of [randsize]
end // end of [local]
(* ****** ****** *)
implement
randperm {n} (n) = let
typedef elt = natLt (n)
val asz = size1_of_int1 (n)
val (pfgc, pfarr | p) = array_ptr_alloc<int> (asz)
val () = loop (pfarr | n, p, 0) where {
fun loop {i:nat | i <= n} {l:addr} .<n-i>. (
pfarr: !array_v (int?, n-i, l) >> array_v (elt, n-i, l)
| n: int n, p: ptr l, i: int i
) :<> void =
if i < n then let
prval (pfat1, pfarr2) = array_v_uncons {int?} (pfarr)
val () = !p := i
val () = loop (pfarr2 | n, p + sizeof<int>, i+1)
prval () = pfarr := array_v_cons {elt} (pfat1, pfarr2)
in
// nothing
end else let
prval () = array_v_unnil {int?} (pfarr)
prval () = pfarr := array_v_nil {elt} ()
in
// nothing
end // end of [if]
// end of [loop]
} // end of [val]
val () = loop (pfarr | n, p, 0) where {
fun loop {i:nat | i <= n} {l:addr} .<n-i>. (
pfarr: !array_v (elt, n-i, l) | n: int n, p: ptr l, i: int i
) :<!ref> void =
if n - i >= 2 then let
val k = randint (n-i)
val () = array_ptr_exch__intsz (!p, 0, k)
prval (pfat1, pfarr2) = array_v_uncons {elt} (pfarr)
val () = loop (pfarr2 | n, p+sizeof<int>, i+1)
prval () = pfarr := array_v_cons {elt} (pfat1, pfarr2)
in
// nothing
end // end of [if]
// end of [loop]
} // end of [val]
in
(pfgc, pfarr | p)
end // end of [randperm]
(* ****** ****** *)
implement
randint_r {n}
(buf, n, res) = let
var d01: double
val _0 = drand48_r (buf, d01)
val r = int_of (d01 * n)
val [r:int] r = int1_of (r)
prval () = __assert () where {
extern prfun __assert (): [0 <= r; r <= n] void
} // end of [prval]
in
if r < n then res := r else res := 0
end // end of [randint_r]
local
staload UN = "prelude/SATS/unsafe.sats"
in // in of [local]
implement
randsize_r {n}
(buf, n, res) = let
var d01: double
val _0 = drand48_r (buf, d01)
val r = ullint_of (d01 * (double_of_size)n)
val r = $UN.cast2size (r)
val [r:int] r = size1_of_size (r)
prval () = __assert () where {
extern prfun __assert (): [0 <= r; r <= n] void
} // end of [prval]
in
if r < n then res := r else res := (size1_of_int1)0
end // end of [randint_r]
end // end of [local]
(* ****** ****** *)
(* end of [random.dats] *)
|