ransyr 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -8
- package/main.cjs +63 -0
- package/main.js +21 -10
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -8,19 +8,21 @@ Python-like random utilities for JavaScript, designed to make your life easier
|
|
|
8
8
|
|
|
9
9
|
## Description
|
|
10
10
|
|
|
11
|
-
`ransyr`
|
|
12
|
-
Daripada harus menulis:
|
|
11
|
+
`ransyr` is a random tools that **imitate python style**.
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
## You Can Use Custom Seed
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
randx.randomseed(27382: number)
|
|
17
|
+
console.log(randx.random())
|
|
16
18
|
```
|
|
17
19
|
|
|
18
|
-
##
|
|
20
|
+
## Usage Example
|
|
19
21
|
|
|
20
22
|
```javascript
|
|
21
|
-
import
|
|
23
|
+
import randx from 'ransyr';
|
|
22
24
|
|
|
23
25
|
const arr = ["apple", "banana", "cherry"];
|
|
24
|
-
console.log(
|
|
25
|
-
console.log(
|
|
26
|
+
console.log(randx.choice(arr));
|
|
27
|
+
console.log(randx.random());
|
|
26
28
|
```
|
package/main.cjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
module.export = class randx {
|
|
4
|
+
|
|
5
|
+
static Rest;
|
|
6
|
+
|
|
7
|
+
static randomseed(seed) {
|
|
8
|
+
let m = 0x80000000;
|
|
9
|
+
let a = 1103515245;
|
|
10
|
+
let c = 12345;
|
|
11
|
+
let state = seed ? seed : Math.floor(Math.random() * (m-1));
|
|
12
|
+
state = (a * state + c) % m;
|
|
13
|
+
randx.Rest = state / (m - 1);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static random() {
|
|
17
|
+
return randx.Rest ? randx.Rest : Math.random();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static sample(arr, k) {
|
|
21
|
+
if (k > arr.length) {
|
|
22
|
+
throw new RangeError("insufficient array length");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const copy = [...arr];
|
|
26
|
+
const result = [];
|
|
27
|
+
|
|
28
|
+
for (let i = 0; i < k; i++) {
|
|
29
|
+
const idx = Math.floor(randx.random() * copy.length);
|
|
30
|
+
result.push(copy[idx]);
|
|
31
|
+
copy.splice(idx, 1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static randint(min, max) {
|
|
38
|
+
if (min > max) [min,
|
|
39
|
+
max] = [max,
|
|
40
|
+
min];
|
|
41
|
+
return Math.floor(randx.random() * (max - min + 1)) + min;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static choice(arr) {
|
|
45
|
+
if (!arr.length) return undefined;
|
|
46
|
+
return arr[Math.floor(randx.random() * arr.length)];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static shuffle(arr) {
|
|
50
|
+
const copy = [...arr];
|
|
51
|
+
for (let i = copy.length - 1; i > 0; i--) {
|
|
52
|
+
const j = Math.floor(randx.random() * (i + 1));
|
|
53
|
+
[copy[i],
|
|
54
|
+
copy[j]] = [copy[j],
|
|
55
|
+
copy[i]];
|
|
56
|
+
}
|
|
57
|
+
return copy;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static chance(percent) {
|
|
61
|
+
return randx.random() * 100 < percent;
|
|
62
|
+
}
|
|
63
|
+
}
|
package/main.js
CHANGED
|
@@ -1,20 +1,32 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
export default class randx {
|
|
4
|
+
|
|
5
|
+
static Rest;
|
|
6
|
+
|
|
7
|
+
static randomseed(seed) {
|
|
8
|
+
let m = 0x80000000;
|
|
9
|
+
let a = 1103515245;
|
|
10
|
+
let c = 12345;
|
|
11
|
+
let state = seed ? seed : Math.floor(Math.random() * (m-1));
|
|
12
|
+
state = (a * state + c) % m;
|
|
13
|
+
randx.Rest = state / (m - 1);
|
|
14
|
+
}
|
|
3
15
|
|
|
4
16
|
static random() {
|
|
5
|
-
return Math.random();
|
|
17
|
+
return randx.Rest ? randx.Rest : Math.random();
|
|
6
18
|
}
|
|
7
19
|
|
|
8
20
|
static sample(arr, k) {
|
|
9
21
|
if (k > arr.length) {
|
|
10
|
-
throw new RangeError("
|
|
22
|
+
throw new RangeError("insufficient array length");
|
|
11
23
|
}
|
|
12
24
|
|
|
13
25
|
const copy = [...arr];
|
|
14
26
|
const result = [];
|
|
15
27
|
|
|
16
28
|
for (let i = 0; i < k; i++) {
|
|
17
|
-
const idx = Math.floor(
|
|
29
|
+
const idx = Math.floor(randx.random() * copy.length);
|
|
18
30
|
result.push(copy[idx]);
|
|
19
31
|
copy.splice(idx, 1);
|
|
20
32
|
}
|
|
@@ -26,18 +38,18 @@ export default class ransyr {
|
|
|
26
38
|
if (min > max) [min,
|
|
27
39
|
max] = [max,
|
|
28
40
|
min];
|
|
29
|
-
return Math.floor(
|
|
41
|
+
return Math.floor(randx.random() * (max - min + 1)) + min;
|
|
30
42
|
}
|
|
31
43
|
|
|
32
44
|
static choice(arr) {
|
|
33
45
|
if (!arr.length) return undefined;
|
|
34
|
-
return arr[Math.floor(
|
|
46
|
+
return arr[Math.floor(randx.random() * arr.length)];
|
|
35
47
|
}
|
|
36
48
|
|
|
37
49
|
static shuffle(arr) {
|
|
38
50
|
const copy = [...arr];
|
|
39
51
|
for (let i = copy.length - 1; i > 0; i--) {
|
|
40
|
-
const j = Math.floor(
|
|
52
|
+
const j = Math.floor(randx.random() * (i + 1));
|
|
41
53
|
[copy[i],
|
|
42
54
|
copy[j]] = [copy[j],
|
|
43
55
|
copy[i]];
|
|
@@ -46,7 +58,6 @@ export default class ransyr {
|
|
|
46
58
|
}
|
|
47
59
|
|
|
48
60
|
static chance(percent) {
|
|
49
|
-
return
|
|
61
|
+
return randx.random() * 100 < percent;
|
|
50
62
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
63
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ransyr",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "python like random utilities for make things easier",
|
|
5
|
-
"main": "main.
|
|
5
|
+
"main": "main.cjs",
|
|
6
|
+
"module": "main.js",
|
|
7
|
+
"type": "module",
|
|
6
8
|
"scripts": {
|
|
7
9
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
10
|
},
|