ransyr 1.2.1 → 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 +22 -10
- package/main.js +21 -10
- package/package.json +1 -1
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
CHANGED
|
@@ -1,19 +1,32 @@
|
|
|
1
|
-
|
|
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
|
+
}
|
|
2
15
|
|
|
3
16
|
static random() {
|
|
4
|
-
return Math.random();
|
|
17
|
+
return randx.Rest ? randx.Rest : Math.random();
|
|
5
18
|
}
|
|
6
19
|
|
|
7
20
|
static sample(arr, k) {
|
|
8
21
|
if (k > arr.length) {
|
|
9
|
-
throw new RangeError("
|
|
22
|
+
throw new RangeError("insufficient array length");
|
|
10
23
|
}
|
|
11
24
|
|
|
12
25
|
const copy = [...arr];
|
|
13
26
|
const result = [];
|
|
14
27
|
|
|
15
28
|
for (let i = 0; i < k; i++) {
|
|
16
|
-
const idx = Math.floor(
|
|
29
|
+
const idx = Math.floor(randx.random() * copy.length);
|
|
17
30
|
result.push(copy[idx]);
|
|
18
31
|
copy.splice(idx, 1);
|
|
19
32
|
}
|
|
@@ -25,18 +38,18 @@ module.exports = class ransyr {
|
|
|
25
38
|
if (min > max) [min,
|
|
26
39
|
max] = [max,
|
|
27
40
|
min];
|
|
28
|
-
return Math.floor(
|
|
41
|
+
return Math.floor(randx.random() * (max - min + 1)) + min;
|
|
29
42
|
}
|
|
30
43
|
|
|
31
44
|
static choice(arr) {
|
|
32
45
|
if (!arr.length) return undefined;
|
|
33
|
-
return arr[Math.floor(
|
|
46
|
+
return arr[Math.floor(randx.random() * arr.length)];
|
|
34
47
|
}
|
|
35
48
|
|
|
36
49
|
static shuffle(arr) {
|
|
37
50
|
const copy = [...arr];
|
|
38
51
|
for (let i = copy.length - 1; i > 0; i--) {
|
|
39
|
-
const j = Math.floor(
|
|
52
|
+
const j = Math.floor(randx.random() * (i + 1));
|
|
40
53
|
[copy[i],
|
|
41
54
|
copy[j]] = [copy[j],
|
|
42
55
|
copy[i]];
|
|
@@ -45,7 +58,6 @@ module.exports = class ransyr {
|
|
|
45
58
|
}
|
|
46
59
|
|
|
47
60
|
static chance(percent) {
|
|
48
|
-
return
|
|
61
|
+
return randx.random() * 100 < percent;
|
|
49
62
|
}
|
|
50
|
-
}
|
|
51
|
-
|
|
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
|
+
}
|