Skip to content

Commit bfab9ad

Browse files
committed
improvement over Random module
1 parent 40a7e8a commit bfab9ad

File tree

11 files changed

+26
-60
lines changed

11 files changed

+26
-60
lines changed

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ let translate loc (prim_name : string)
434434
(* Js_of_lam_array.make_array NA Pgenarray [] *)
435435
(* ] *)
436436
| "caml_sys_time"
437-
| "caml_sys_random_seed"
438437
| "caml_sys_getenv"
439438
| "caml_sys_system_command"
440439
| "caml_sys_getcwd" (* check browser or nodejs *)

jscomp/runtime/caml_sys.ml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ let os_type : unit -> string = [%raw{|function(_){
4949
}|}]
5050
(* TODO: improve [js_pass_scope] to avoid remove unused n here *)
5151

52-
external now : unit -> float = "" [@@bs.val "Date.now"]
5352

5453

5554
(* let caml_initial_time = now () *. 0.001 *)
@@ -65,14 +64,6 @@ let caml_sys_time () =
6564
else uptime [%raw{|process|}] ()
6665

6766

68-
external random : unit -> float = "Math.random" [@@bs.val]
69-
70-
let caml_sys_random_seed () : nativeint array =
71-
[|
72-
Caml_nativeint_extern.of_float
73-
((Caml_nativeint_extern.to_float (Caml_nativeint_extern.logxor (Caml_nativeint_extern.of_float (now ()))
74-
0xffffffffn)) *. random ()) |]
75-
7667

7768

7869
(*

jscomp/runtime/caml_sys.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ val caml_sys_time : unit -> float
3030

3131
val os_type : unit -> string
3232

33-
val caml_sys_random_seed : unit -> nativeint array
34-
3533
val caml_sys_system_command : string -> int
3634

3735
val caml_sys_getcwd : unit -> string

jscomp/runtime/release.ninja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ build runtime/caml_splice_call.cmj : cc_cmi runtime/caml_splice_call.ml | runtim
5353
build runtime/caml_splice_call.cmi : cc runtime/caml_splice_call.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5454
build runtime/caml_string.cmj : cc_cmi runtime/caml_string.ml | runtime/caml_string.cmi runtime/caml_string_extern.cmj
5555
build runtime/caml_string.cmi : cc runtime/caml_string.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
56-
build runtime/caml_sys.cmj : cc_cmi runtime/caml_sys.ml | runtime/caml_array_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_sys.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj
56+
build runtime/caml_sys.cmj : cc_cmi runtime/caml_sys.ml | runtime/caml_array_extern.cmj runtime/caml_sys.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj
5757
build runtime/caml_sys.cmi : cc runtime/caml_sys.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5858
build runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj : cc runtime/caml_array_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5959
build runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj : cc runtime/caml_exceptions.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj

jscomp/stdlib-406/random.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
passes all the Diehard tests.
2626
*)
2727

28-
external random_seed: unit -> int array = "caml_sys_random_seed"
28+
let random_seed: unit -> int array = fun _ ->
29+
let seed :int = [%raw "Math.floor(Math.random()*0x7fff_ffff)"] in
30+
[|seed|]
2931

3032
module State = struct
3133

@@ -124,12 +126,12 @@ module State = struct
124126
then invalid_arg "Random.int64"
125127
else int64aux s bound
126128

127-
129+
#if 0 then
128130
let nativeint =
129131
if Nativeint.size = 32
130132
then fun s bound -> Nativeint.of_int32 (int32 s (Nativeint.to_int32 bound))
131133
else fun s bound -> Int64.to_nativeint (int64 s (Int64.of_nativeint bound))
132-
134+
#end
133135

134136
(* Returns a float 0 <= x <= 1 with at most 60 bits of precision. *)
135137
let rawfloat s =
@@ -166,7 +168,9 @@ let default = {
166168
let bits () = State.bits default
167169
let int bound = State.int default bound
168170
let int32 bound = State.int32 default bound
171+
#if 0 then
169172
let nativeint bound = State.nativeint default bound
173+
#end
170174
let int64 bound = State.int64 default bound
171175
let float scale = State.float default scale
172176
let bool () = State.bool default

jscomp/stdlib-406/random.mli

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ val int : int -> int
4545
val int32 : Int32.t -> Int32.t
4646
(** [Random.int32 bound] returns a random integer between 0 (inclusive)
4747
and [bound] (exclusive). [bound] must be greater than 0. *)
48-
48+
#if 0 then
4949
val nativeint : Nativeint.t -> Nativeint.t
5050
(** [Random.nativeint bound] returns a random integer between 0 (inclusive)
5151
and [bound] (exclusive). [bound] must be greater than 0. *)
52-
52+
#end
5353
val int64 : Int64.t -> Int64.t
5454
(** [Random.int64 bound] returns a random integer between 0 (inclusive)
5555
and [bound] (exclusive). [bound] must be greater than 0. *)
@@ -90,7 +90,9 @@ module State : sig
9090
val bits : t -> int
9191
val int : t -> int -> int
9292
val int32 : t -> Int32.t -> Int32.t
93+
#if 0 then
9394
val nativeint : t -> Nativeint.t -> Nativeint.t
95+
#end
9496
val int64 : t -> Int64.t -> Int64.t
9597
val float : t -> float -> float
9698
val bool : t -> bool

jscomp/stdlib-406/release.ninja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ build stdlib-406/printf.cmj : cc_cmi stdlib-406/printf.ml | stdlib-406/buffer.cm
9191
build stdlib-406/printf.cmi : cc stdlib-406/printf.mli | stdlib-406/buffer.cmi stdlib-406/pervasives.cmj others
9292
build stdlib-406/queue.cmj : cc_cmi stdlib-406/queue.ml | stdlib-406/queue.cmi others
9393
build stdlib-406/queue.cmi : cc stdlib-406/queue.mli | stdlib-406/pervasives.cmj others
94-
build stdlib-406/random.cmj : cc_cmi stdlib-406/random.ml | stdlib-406/array.cmj stdlib-406/char.cmj stdlib-406/digest.cmj stdlib-406/int32.cmj stdlib-406/int64.cmj stdlib-406/nativeint.cmj stdlib-406/pervasives.cmj stdlib-406/random.cmi stdlib-406/string.cmj others
95-
build stdlib-406/random.cmi : cc stdlib-406/random.mli | stdlib-406/int32.cmi stdlib-406/int64.cmi stdlib-406/nativeint.cmi stdlib-406/pervasives.cmj others
94+
build stdlib-406/random.cmj : cc_cmi stdlib-406/random.ml | stdlib-406/array.cmj stdlib-406/char.cmj stdlib-406/digest.cmj stdlib-406/int32.cmj stdlib-406/int64.cmj stdlib-406/pervasives.cmj stdlib-406/random.cmi stdlib-406/string.cmj others
95+
build stdlib-406/random.cmi : cc stdlib-406/random.mli | stdlib-406/int32.cmi stdlib-406/int64.cmi stdlib-406/pervasives.cmj others
9696
build stdlib-406/scanf.cmj : cc_cmi stdlib-406/scanf.ml | stdlib-406/buffer.cmj stdlib-406/bytes.cmj stdlib-406/camlinternalFormat.cmj stdlib-406/camlinternalFormatBasics.cmj stdlib-406/list.cmj stdlib-406/pervasives.cmj stdlib-406/printf.cmj stdlib-406/scanf.cmi stdlib-406/string.cmj others
9797
build stdlib-406/scanf.cmi : cc stdlib-406/scanf.mli | stdlib-406/pervasives.cmi stdlib-406/pervasives.cmj others
9898
build stdlib-406/set.cmj : cc_cmi stdlib-406/set.ml | stdlib-406/list.cmj stdlib-406/set.cmi others

lib/es6/caml_sys.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ function caml_sys_time(param) {
3535
}
3636
}
3737

38-
function caml_sys_random_seed(param) {
39-
return [((Date.now() | 0) ^ 4294967295) * Math.random() | 0];
40-
}
41-
4238
function caml_sys_system_command(_cmd) {
4339
return 127;
4440
}
@@ -98,7 +94,6 @@ export {
9894
caml_sys_getenv ,
9995
caml_sys_time ,
10096
os_type ,
101-
caml_sys_random_seed ,
10297
caml_sys_system_command ,
10398
caml_sys_getcwd ,
10499
caml_sys_get_argv ,

lib/es6/random.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11

22

33
import * as $$Array from "./array.js";
4-
import * as Curry from "./curry.js";
54
import * as Int32 from "./int32.js";
65
import * as Int64 from "./int64.js";
76
import * as Digest from "./digest.js";
8-
import * as Caml_sys from "./caml_sys.js";
9-
import * as Nativeint from "./nativeint.js";
107
import * as Caml_array from "./caml_array.js";
118
import * as Caml_int64 from "./caml_int64.js";
129
import * as Caml_string from "./caml_string.js";
1310

11+
function random_seed(param) {
12+
return [(Math.floor(Math.random()*0x7fff_ffff))];
13+
}
14+
1415
function assign(st1, st2) {
1516
$$Array.blit(st2.st, 0, st1.st, 0, 55);
1617
st1.idx = st2.idx;
@@ -52,7 +53,7 @@ function make(seed) {
5253
}
5354

5455
function make_self_init(param) {
55-
return make(Caml_sys.caml_sys_random_seed(undefined));
56+
return make(random_seed(undefined));
5657
}
5758

5859
function copy(s) {
@@ -132,10 +133,6 @@ function int64(s, bound) {
132133
};
133134
}
134135

135-
var nativeint = Nativeint.size === 32 ? int32 : (function (s, bound) {
136-
return Caml_int64.to_int32(int64(s, Caml_int64.of_int32(bound)));
137-
});
138-
139136
function rawfloat(s) {
140137
var r1 = bits(s);
141138
var r2 = bits(s);
@@ -223,10 +220,6 @@ function int32$1(bound) {
223220
return int32($$default, bound);
224221
}
225222

226-
function nativeint$1(bound) {
227-
return Curry._2(nativeint, $$default, bound);
228-
}
229-
230223
function int64$1(bound) {
231224
return int64($$default, bound);
232225
}
@@ -248,7 +241,7 @@ function init(seed) {
248241
}
249242

250243
function self_init(param) {
251-
return full_init$1(Caml_sys.caml_sys_random_seed(undefined));
244+
return full_init$1(random_seed(undefined));
252245
}
253246

254247
function get_state(param) {
@@ -266,7 +259,6 @@ var State = {
266259
bits: bits,
267260
$$int: $$int,
268261
int32: int32,
269-
nativeint: nativeint,
270262
int64: int64,
271263
$$float: $$float,
272264
bool: bool
@@ -279,7 +271,6 @@ export {
279271
bits$1 as bits,
280272
$$int$1 as $$int,
281273
int32$1 as int32,
282-
nativeint$1 as nativeint,
283274
int64$1 as int64,
284275
$$float$1 as $$float,
285276
bool$1 as bool,

lib/js/caml_sys.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ function caml_sys_time(param) {
3535
}
3636
}
3737

38-
function caml_sys_random_seed(param) {
39-
return [((Date.now() | 0) ^ 4294967295) * Math.random() | 0];
40-
}
41-
4238
function caml_sys_system_command(_cmd) {
4339
return 127;
4440
}
@@ -97,7 +93,6 @@ function caml_sys_file_exists(_s) {
9793
exports.caml_sys_getenv = caml_sys_getenv;
9894
exports.caml_sys_time = caml_sys_time;
9995
exports.os_type = os_type;
100-
exports.caml_sys_random_seed = caml_sys_random_seed;
10196
exports.caml_sys_system_command = caml_sys_system_command;
10297
exports.caml_sys_getcwd = caml_sys_getcwd;
10398
exports.caml_sys_get_argv = caml_sys_get_argv;

0 commit comments

Comments
 (0)