This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,6 @@
_build/
_tests/
*.native
*.byte
*.install
.merlin

View file

@ -0,0 +1,17 @@
language: c
install: wget https://raw.githubusercontent.com/ocaml/ocaml-ci-scripts/master/.travis-docker.sh
script: bash -ex .travis-docker.sh
sudo: false
services:
- docker
env:
global:
- PACKAGE="randomconv"
- DISTRO=alpine
- TESTS=false
matrix:
- OCAML_VERSION=4.09
- OCAML_VERSION=4.08
- OCAML_VERSION=4.07
notifications:
email: false

View file

@ -0,0 +1,19 @@
## v0.2.0 (2024-03-05)
* Use (int -> string) instead of (int -> Cstruct.t) as generator
## v0.1.3 (2020-02-22)
* `int` now allows a bound of 1 (returning 0)
## 0.1.2 (2019-02-17)
* minor fixes to documentation (reported in #3)
## 0.1.1 (2019-02-16)
* move build system to dune
## 0.1.0 (2017-01-17)
* initial release

View file

@ -0,0 +1,16 @@
(*
* Copyright (c) 2016 Hannes Mehnert <hannes@mehnert.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)

View file

@ -0,0 +1,16 @@
# Randomconv - convert from random bytes to random native numbers
v0.2.0
Given a function which produces random byte vectors, convert it to
a number of your choice (int8/int16/int32/int64/int/float).
## Documentation
[![Build Status](https://travis-ci.org/hannesm/randomconv.svg?branch=master)](https://travis-ci.org/hannesm/randomconv)
[API Documentation](https://hannesm.github.io/randomconv/doc/) is available online.
## Installation
`opam install randomconv` installs the latest released version.

View file

@ -0,0 +1,4 @@
(library
(name randomconv)
(public_name randomconv)
(modules randomconv))

View file

@ -0,0 +1,3 @@
(lang dune 1.0)
(name randomconv)
(version v0.2.0)

View file

@ -0,0 +1,58 @@
(* revise once lower bound OCaml 4.13 *)
let string_get_uint8 s idx =
Bytes.get_uint8 (Bytes.unsafe_of_string s) idx
let string_get_uint16 s idx =
Bytes.get_uint16_le (Bytes.unsafe_of_string s) idx
let string_get_int32 s idx =
Bytes.get_int32_le (Bytes.unsafe_of_string s) idx
let string_get_int64 s idx =
Bytes.get_int64_le (Bytes.unsafe_of_string s) idx
type int8 = int
let int8 g = string_get_uint8 (g 1) 0
type int16 = int
let int16 g = string_get_uint16 (g 2) 0
let int32 g = string_get_int32 (g 4) 0
let int64 g = string_get_int64 (g 8) 0
let bitmask n =
let rec go c = function
| 0 -> c
| n -> go (c lsl 1) (n lsr 1)
in
go 1 n - 1
let rec int ?(bound = max_int) g =
if bound <= 0 then invalid_arg "bound smaller or equal 0 not supported" ;
if bound = 1 then 0
else
let r =
if bound <= 256 then
int8 g
else if bound <= 65536 then
int16 g
else
match Sys.word_size with
| 32 -> Int32.to_int (int32 g)
| 64 -> Int64.to_int (int64 g)
| _ -> invalid_arg "unknown word size"
in
let r = r land bitmask (pred bound) in
if r < bound then r else int ~bound g
let float ?(bound = 1.) g =
if bound <= 0. then invalid_arg "bound smaller or equal 0 not supported" ;
let scale = float_of_int max_int
and r1 = int g
and r2 = int g
in
bound *. ((float_of_int r1 /. scale +. float_of_int r2) /. scale)

View file

@ -0,0 +1,35 @@
(** Randomconv - random in various representations
Given a generator for random byte vectors, this convenience library converts
the output into common number representations: int8, int16, int32, int64,
int, and float.
{e v0.2.0 - {{:https://github.com/hannesm/randomconv }homepage}}
*)
type int8 = int
(** [int8 g] is [r], a random [int8] using the generator [g]. *)
val int8 : (int -> string) -> int8
type int16 = int
(** [int16 g] is [r], a random [int16] using the generator [g]. *)
val int16 : (int -> string) -> int16
(** [int32 g] is [r], a random [int32] using the generator [g]. *)
val int32 : (int -> string) -> int32
(** [int64 g] is [r], a random [int64] using the generator [g]. *)
val int64 : (int -> string) -> int64
(** [int ~bound g] is [r], a random [int] between inclusive 0 and exclusive
[bound] (defaults to [max_int]), using the generator [g]. [int] raises
[Invalid_argument] if the supplied [bound] is smaller or equal to 0. *)
val int : ?bound:int -> (int -> string) -> int
(** [float ~bound g] is [r], a random [float] between inclusive 0.0 and
exclusive [bound] (defaults to [1.0]), using the generator [g]. [float]
raises [Invalid_argument] if the supplied [bound] is smaller or equal to
0. *)
val float : ?bound:float -> (int -> string) -> float

View file

@ -0,0 +1,22 @@
version: "0.2.0"
opam-version: "2.0"
maintainer: "Hannes Mehnert <hannes@mehnert.org>"
authors: "Hannes Mehnert <hannes@mehnert.org>"
license: "ISC"
homepage: "https://github.com/hannesm/randomconv"
doc: "https://hannesm.github.io/randomconv/doc"
bug-reports: "https://github.com/hannesm/randomconv/issues"
depends: [
"ocaml" {>= "4.04.2"}
"dune"
]
build: [
["dune" "subst"] {dev}
["dune" "build" "-p" name "-j" jobs]
]
dev-repo: "git+https://github.com/hannesm/randomconv.git"
synopsis: "Convert from random byte vectors (int -> string) to random native numbers"
description: """
Given a function which produces random byte vectors, convert it to
a number of your choice (int8/int16/int32/int64/int/float).
"""