Skip to content

Added random_int to Core__Math #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Core__Math.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@

var Constants = {};

var Int = {};
function floor(f) {
return Math.floor(f) | 0;
}

function random(min, max) {
var f = Math.random() * (max - min | 0);
return (Math.floor(f) | 0) + min | 0;
}

var Int = {
floor: floor,
random: random
};

export {
Constants ,
Expand Down
27 changes: 15 additions & 12 deletions src/Core__Math.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ module Constants = {
@val external sqrt2: float = "Math.SQRT2"
}

module Int = {
@val external abs: int => int = "Math.abs"
@val external clz32: int => int = "Math.clz32"
@val external imul: (int, int) => int = "Math.imul"
@val external min: (int, int) => int = "Math.min"
@variadic @val external minMany: array<int> => int = "Math.min"
@val external max: (int, int) => int = "Math.max"
@variadic @val external maxMany: array<int> => int = "Math.max"
@val external pow: (int, ~exp: int) => int = "Math.pow"
@val external sign: int => int = "Math.sign"
}

@val external abs: float => float = "Math.abs"
@val external acos: float => float = "Math.acos"
@val external acosh: float => float = "Math.acosh"
Expand Down Expand Up @@ -57,3 +45,18 @@ module Int = {
@val external tan: float => float = "Math.tan"
@val external tanh: float => float = "Math.tanh"
@val external trunc: float => float = "Math.trunc"

module Int = {
@val external abs: int => int = "Math.abs"
@val external clz32: int => int = "Math.clz32"
@val external imul: (int, int) => int = "Math.imul"
@val external min: (int, int) => int = "Math.min"
@variadic @val external minMany: array<int> => int = "Math.min"
@val external max: (int, int) => int = "Math.max"
@variadic @val external maxMany: array<int> => int = "Math.max"
@val external pow: (int, ~exp: int) => int = "Math.pow"
@val external sign: int => int = "Math.sign"
let floor: float => int = f => f->floor->Core__Float.toInt
let random: (int, int) => int = (min, max) =>
floor(random() *. Core__Int.toFloat(max - min)) + min
}
33 changes: 33 additions & 0 deletions src/Core__Math.resi
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,39 @@ module Int: {
*/
@val
external sign: int => int = "Math.sign"

/**
floor(v) returns the largest `int` less than or equal to the argument;
the result is pinned to the range of the `int` data type: -2147483648 to 2147483647.
See [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor)
on MDN.

## Examples

```rescript
Math.Int.floor(3.7) == 3
Math.Int.floor(3.0) == 3
Math.Int.floor(-3.1) == -4
Math.Int.floor(-1.0e15) == -2147483648
Math.Int.floor(1.0e15) == 2147483647
```
*/
let floor: float => int

/**
`random(minVal, maxVal)` returns a random integer number in the half-closed interval [minVal, maxVal).
See [`Math.random`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)
on MDN.

## Examples

```rescript
Math.Int.random(2, 5) == 4
Math.Int.random(505, 2000) == 1276
Math.Int.random(-7, -2) == -4
```
*/
let random: (int, int) => int
}

/**
Expand Down