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.
Files changed (4) hide show
  1. package/README.md +10 -8
  2. package/main.cjs +22 -10
  3. package/main.js +21 -10
  4. 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` dibuat untuk mempermudah penggunaan random di JavaScript, **meniru gaya Python**.
12
- Daripada harus menulis:
11
+ `ransyr` is a random tools that **imitate python style**.
13
12
 
14
- ```javascript
15
- const choice = arr[Math.floor(Math.random() * arr.length)];
13
+ ## You Can Use Custom Seed
14
+
15
+ ```typescript
16
+ randx.randomseed(27382: number)
17
+ console.log(randx.random())
16
18
  ```
17
19
 
18
- ## Contoh Penggunaan
20
+ ## Usage Example
19
21
 
20
22
  ```javascript
21
- import ransyr from 'ransyr';
23
+ import randx from 'ransyr';
22
24
 
23
25
  const arr = ["apple", "banana", "cherry"];
24
- console.log(ransyr.choice(arr));
25
- console.log(ransyr.random());
26
+ console.log(randx.choice(arr));
27
+ console.log(randx.random());
26
28
  ```
package/main.cjs CHANGED
@@ -1,19 +1,32 @@
1
- module.exports = class ransyr {
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("sample size larger than population");
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(Math.random() * copy.length);
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(Math.random() * (max - min + 1)) + min;
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(Math.random() * arr.length)];
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(Math.random() * (i + 1));
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 Math.random() * 100 < percent;
61
+ return randx.random() * 100 < percent;
49
62
  }
50
- }
51
-
63
+ }
package/main.js CHANGED
@@ -1,20 +1,32 @@
1
1
 
2
- export default class ransyr {
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("sample size larger than population");
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(Math.random() * copy.length);
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(Math.random() * (max - min + 1)) + min;
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(Math.random() * arr.length)];
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(Math.random() * (i + 1));
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 Math.random() * 100 < percent;
61
+ return randx.random() * 100 < percent;
50
62
  }
51
- }
52
-
63
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ransyr",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "python like random utilities for make things easier",
5
5
  "main": "main.cjs",
6
6
  "module": "main.js",