@raindrops-on-roses/number-numbers-from-seed 0.0.1
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 +117 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/package.json +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# @raindrops-on-roses/numbers-from-seed
|
|
2
|
+
|
|
3
|
+
`numbersFromSeed` utility from [raindrops-on-roses](https://www.npmjs.com/package/raindrops-on-roses).
|
|
4
|
+
|
|
5
|
+
Generates a deterministic array of pseudo-random numbers from a seed. Using the same seed and options always produces the same sequence.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @raindrops-on-roses/numbers-from-seed
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { numbersFromSeed } from "@raindrops-on-roses/numbers-from-seed";
|
|
17
|
+
|
|
18
|
+
numbersFromSeed({
|
|
19
|
+
count: 5,
|
|
20
|
+
seed: "example",
|
|
21
|
+
});
|
|
22
|
+
// Deterministic sequence for this seed and configuration
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
You can control the generated range, rounding behavior, and sort direction:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
numbersFromSeed({
|
|
29
|
+
count: 5,
|
|
30
|
+
seed: "example",
|
|
31
|
+
trend: "up",
|
|
32
|
+
multiplicator: 100,
|
|
33
|
+
});
|
|
34
|
+
// Returns the same sorted sequence every time
|
|
35
|
+
|
|
36
|
+
numbersFromSeed({
|
|
37
|
+
count: 3,
|
|
38
|
+
seed: 42,
|
|
39
|
+
multiplicator: 10,
|
|
40
|
+
rounded: false,
|
|
41
|
+
});
|
|
42
|
+
// Returns deterministic decimal values between 0 and 10
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
numbersFromSeed({
|
|
49
|
+
count,
|
|
50
|
+
seed,
|
|
51
|
+
trend,
|
|
52
|
+
multiplicator,
|
|
53
|
+
rounded,
|
|
54
|
+
}: {
|
|
55
|
+
count: number;
|
|
56
|
+
seed?: string | number;
|
|
57
|
+
trend?: "up" | "down" | null;
|
|
58
|
+
multiplicator?: number;
|
|
59
|
+
rounded?: boolean;
|
|
60
|
+
}): number[]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Parameters
|
|
64
|
+
|
|
65
|
+
- `count` - Number of values to generate.
|
|
66
|
+
- `seed` - Seed used to generate the deterministic sequence.
|
|
67
|
+
- `trend` - Optional sorting direction. Use `"up"` for ascending order, `"down"` for descending order, or `null` to keep the generated order.
|
|
68
|
+
- `multiplicator` - Multiplier applied to each generated value. Defaults to `1`.
|
|
69
|
+
- `rounded` - Whether generated values should be rounded to integers. Defaults to `true`.
|
|
70
|
+
|
|
71
|
+
### Returns
|
|
72
|
+
|
|
73
|
+
An array of deterministic pseudo-random numbers.
|
|
74
|
+
|
|
75
|
+
Values are generated between `0` and `multiplicator`, then optionally rounded and sorted.
|
|
76
|
+
|
|
77
|
+
## Deterministic output
|
|
78
|
+
|
|
79
|
+
The same seed and configuration always produce the same sequence:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const first = numbersFromSeed({
|
|
83
|
+
count: 5,
|
|
84
|
+
seed: "example",
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const second = numbersFromSeed({
|
|
88
|
+
count: 5,
|
|
89
|
+
seed: "example",
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// first and second contain the same values in the same order
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## With the complete library
|
|
96
|
+
|
|
97
|
+
You can also install the complete `raindrops-on-roses` package:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
npm install raindrops-on-roses
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
and import `numbersFromSeed` from the umbrella package:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { numbersFromSeed } from "raindrops-on-roses";
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Lore
|
|
110
|
+
|
|
111
|
+
> All the flowers of all the tomorrows are in the seeds of today.
|
|
112
|
+
|
|
113
|
+
(Native American proverb)
|
|
114
|
+
|
|
115
|
+
## Repository
|
|
116
|
+
|
|
117
|
+
[graphieros/raindrops-on-roses](https://github.com/graphieros/raindrops-on-roses)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a deterministic array of pseudo-random numbers from a seed.
|
|
3
|
+
*
|
|
4
|
+
* ---
|
|
5
|
+
*
|
|
6
|
+
* Using the same seed and options produces the same sequence.
|
|
7
|
+
* Values are generated between `0` and `multiplicator`, and can optionally
|
|
8
|
+
* be rounded or sorted.
|
|
9
|
+
*
|
|
10
|
+
* @param count - Number of values to generate.
|
|
11
|
+
* @param seed - Seed used to generate the deterministic sequence.
|
|
12
|
+
* @param trend - Optional sorting direction for the generated values.
|
|
13
|
+
* @param multiplicator - Multiplier applied to each generated value. Defaults to `1`.
|
|
14
|
+
* @param rounded - Whether generated values should be rounded to integers. Defaults to `true`.
|
|
15
|
+
* @returns An array of numbers
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const numbers = numbersFromSeed({
|
|
20
|
+
* count: 5,
|
|
21
|
+
* seed: "example",
|
|
22
|
+
* trend: "up",
|
|
23
|
+
* multiplicator: 100,
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Returns the same sequence every time for this seed and config.
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* ---
|
|
30
|
+
*
|
|
31
|
+
* Lore: All the flowers of all the tomorrows are in the seeds of today (Native american proverb)
|
|
32
|
+
*/
|
|
33
|
+
export declare function numbersFromSeed({ count, seed, trend, multiplicator, rounded, }: {
|
|
34
|
+
count: number;
|
|
35
|
+
seed?: string | number;
|
|
36
|
+
trend?: "up" | "down" | null;
|
|
37
|
+
multiplicator?: number;
|
|
38
|
+
rounded?: boolean;
|
|
39
|
+
}): number[];
|
|
40
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,eAAe,CAAC,EAC9B,KAAK,EACL,IAAI,EACJ,KAAY,EACZ,aAAiB,EACjB,OAAc,GACf,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,KAAK,CAAC,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAAC;IAC7B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,GAAG,MAAM,EAAE,CAwBX"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
function e({ count: e, seed: t, trend: n = null, multiplicator: r = 1, rounded: i = !0 }) {
|
|
3
|
+
let a = String(t), o = 2166136261;
|
|
4
|
+
for (let e = 0; e < a.length; e += 1) o ^= a.charCodeAt(e), o = Math.imul(o, 16777619);
|
|
5
|
+
let s = o >>> 0, c = Array.from({ length: e }, () => {
|
|
6
|
+
s += 1831565813;
|
|
7
|
+
let e = s;
|
|
8
|
+
e = Math.imul(e ^ e >>> 15, e | 1), e ^= e + Math.imul(e ^ e >>> 7, e | 61);
|
|
9
|
+
let t = ((e ^ e >>> 14) >>> 0) / 4294967296 * r;
|
|
10
|
+
return i ? Math.round(t) : t;
|
|
11
|
+
});
|
|
12
|
+
return n === "up" ? c.sort((e, t) => e - t) : n === "down" ? c.sort((e, t) => t - e) : c;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { e as numbersFromSeed };
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Generates a deterministic array of pseudo-random numbers from a seed.\n *\n * ---\n *\n * Using the same seed and options produces the same sequence.\n * Values are generated between `0` and `multiplicator`, and can optionally\n * be rounded or sorted.\n *\n * @param count - Number of values to generate.\n * @param seed - Seed used to generate the deterministic sequence.\n * @param trend - Optional sorting direction for the generated values.\n * @param multiplicator - Multiplier applied to each generated value. Defaults to `1`.\n * @param rounded - Whether generated values should be rounded to integers. Defaults to `true`.\n * @returns An array of numbers\n *\n * @example\n * ```ts\n * const numbers = numbersFromSeed({\n * count: 5,\n * seed: \"example\",\n * trend: \"up\",\n * multiplicator: 100,\n * });\n *\n * // Returns the same sequence every time for this seed and config.\n * ```\n *\n * ---\n *\n * Lore: All the flowers of all the tomorrows are in the seeds of today (Native american proverb)\n */\nexport function numbersFromSeed({\n count,\n seed,\n trend = null,\n multiplicator = 1,\n rounded = true,\n}: {\n count: number;\n seed?: string | number;\n trend?: \"up\" | \"down\" | null;\n multiplicator?: number;\n rounded?: boolean;\n}): number[] {\n const seedString = String(seed);\n let hash = 2166136261;\n for (let i = 0; i < seedString.length; i += 1) {\n hash ^= seedString.charCodeAt(i);\n hash = Math.imul(hash, 16777619);\n }\n let state = hash >>> 0;\n const random = () => {\n state += 0x6d2b79f5;\n let t = state;\n t = Math.imul(t ^ (t >>> 15), t | 1);\n t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n const res = (((t ^ (t >>> 14)) >>> 0) / 4294967296) * multiplicator;\n return rounded ? Math.round(res) : res;\n };\n const numbers = Array.from({ length: count }, random);\n if (trend === \"up\") {\n return numbers.sort((a, b) => a - b);\n }\n if (trend === \"down\") {\n return numbers.sort((a, b) => b - a);\n }\n return numbers;\n}\n"],"mappings":";AAgCA,SAAgB,EAAgB,EAC9B,UACA,SACA,WAAQ,MACR,mBAAgB,GAChB,aAAU,MAOC;CACX,IAAM,IAAa,OAAO,CAAI,GAC1B,IAAO;CACX,KAAK,IAAI,IAAI,GAAG,IAAI,EAAW,QAAQ,KAAK,GAE1C,AADA,KAAQ,EAAW,WAAW,CAAC,GAC/B,IAAO,KAAK,KAAK,GAAM,QAAQ;CAEjC,IAAI,IAAQ,MAAS,GASf,IAAU,MAAM,KAAK,EAAE,QAAQ,EAAM,SARtB;EACnB,KAAS;EACT,IAAI,IAAI;EAER,AADA,IAAI,KAAK,KAAK,IAAK,MAAM,IAAK,IAAI,CAAC,GACnC,KAAK,IAAI,KAAK,KAAK,IAAK,MAAM,GAAI,IAAI,EAAE;EACxC,IAAM,MAAS,IAAK,MAAM,QAAS,KAAK,aAAc;EACtD,OAAO,IAAU,KAAK,MAAM,CAAG,IAAI;CACrC,CACoD;CAOpD,OANI,MAAU,OACL,EAAQ,MAAM,GAAG,MAAM,IAAI,CAAC,IAEjC,MAAU,SACL,EAAQ,MAAM,GAAG,MAAM,IAAI,CAAC,IAE9B;AACT"}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@raindrops-on-roses/number-numbers-from-seed",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "vite build && tsc -p tsconfig.json"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/graphieros/raindrops-on-roses.git",
|
|
25
|
+
"directory": "packages/number-numbers-from-seed"
|
|
26
|
+
}
|
|
27
|
+
}
|