py-random-js 1.0.0
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 +71 -0
- package/package.json +9 -0
- package/randomizer.js +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Randomizer.js
|
|
2
|
+
|
|
3
|
+
**Stop wrestling with `Math.floor(Math.random() * ...)`!**
|
|
4
|
+
|
|
5
|
+
Are you coming from Python and missing the simplicity of the `random` module? Or are you just tired of writing the same boilerplate code every time you need a random integer or a shuffled array?
|
|
6
|
+
|
|
7
|
+
**Randomizer.js** is a lightweight, zero-dependency utility that brings Python-style randomness to the JavaScript world.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
* **Python-like API**: Simple, clean, and intuitive methods.
|
|
14
|
+
* **Game-Dev Ready**: Includes shuffling, coin flips, and percentage chances.
|
|
15
|
+
* **Modern JS**: Written as an ES Module for maximum compatibility.
|
|
16
|
+
* **Lightweight**: Only what you need, nothing you don't.
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install py-random-js
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
import random from 'py-random-js';
|
|
30
|
+
|
|
31
|
+
// 1. Get a random integer between 0 and 50 (inclusive)
|
|
32
|
+
console.log(random.int(50));
|
|
33
|
+
|
|
34
|
+
// 2. Pick a random element from an array
|
|
35
|
+
const items = ['Sword', 'Shield', 'Potion', 'Spell'];
|
|
36
|
+
console.log(random.array(items));
|
|
37
|
+
|
|
38
|
+
// 3. Shuffle an array (Fisher-Yates algorithm)
|
|
39
|
+
const deck = [1, 2, 3, 4, 5];
|
|
40
|
+
console.log(random.shuffle(deck));
|
|
41
|
+
|
|
42
|
+
// 4. Flip a coin (50/50 chance)
|
|
43
|
+
if (random.coinFlip()) {
|
|
44
|
+
console.log("Heads!");
|
|
45
|
+
} else {
|
|
46
|
+
console.log("Tails!");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 5. Roll for a specific chance (e.g., 15% for a Critical Hit)
|
|
50
|
+
if (random.chance(15)) {
|
|
51
|
+
console.log("CRITICAL HIT!");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Generate a string of a certain length containing random characters (e.g., generate an ID)
|
|
55
|
+
console.log(`Random generated string: ${random.string(16)}`) // Random generated string: gfCFSYMsB4ygLEsW
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## API Reference
|
|
59
|
+
|
|
60
|
+
| Method | Arguments | Description |
|
|
61
|
+
| :---: | :---: | :---: |
|
|
62
|
+
| .int(max) | number | Returns a random integer from 0 to max (inclusive). |
|
|
63
|
+
| .array(array) | Array | Returns one random element from the provided array. |
|
|
64
|
+
| .shuffle(array) | Array | Returns a NEW shuffled version of the array. |
|
|
65
|
+
| .coinFlip() | none | Returns true or false with a 50% probability. |
|
|
66
|
+
| .chance(n) | number | Returns true with a n percent probability (0-100). |
|
|
67
|
+
| .string(n) | number | Returns a string of length n containing random characters. |
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
Author: bodutop
|
|
71
|
+
License: MIT
|
package/package.json
ADDED
package/randomizer.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
class Randomizer{
|
|
2
|
+
int(targetValue) {
|
|
3
|
+
return Math.floor(Math.random() * (targetValue + 1));
|
|
4
|
+
}
|
|
5
|
+
array(targetArray) {
|
|
6
|
+
const randomElement = Math.floor(Math.random() * targetArray.length);
|
|
7
|
+
return targetArray[randomElement];
|
|
8
|
+
}
|
|
9
|
+
string(targetLength) {
|
|
10
|
+
const chars = 'AaBbCcDdEeFfGgHhJjKkLlMmNnoPpQqRrSsTtUuVvWwXxYyZz23456789';
|
|
11
|
+
let string = '';
|
|
12
|
+
for (let i = targetLength; i > 0; i--) {
|
|
13
|
+
let char = chars[Math.floor(Math.random() * chars.length)];
|
|
14
|
+
string = string + char;
|
|
15
|
+
}
|
|
16
|
+
return string;
|
|
17
|
+
}
|
|
18
|
+
shuffle(targetArray) {
|
|
19
|
+
for (let i = targetArray.length - 1; i > 0; i--) {
|
|
20
|
+
let j = Math.floor(Math.random() * (i + 1));
|
|
21
|
+
[targetArray[i], targetArray[j]] = [targetArray[j], targetArray[i]];
|
|
22
|
+
}
|
|
23
|
+
return targetArray;
|
|
24
|
+
}
|
|
25
|
+
coinFlip() {
|
|
26
|
+
return Math.random() < 0.5
|
|
27
|
+
}
|
|
28
|
+
chance(targetPercentage) {
|
|
29
|
+
return Math.random() < (targetPercentage / 100);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export default new Randomizer();
|
|
33
|
+
|