Skip to content

Commit ce0b65e

Browse files
authored
Create randomWithChances.js
1 parent f3e9943 commit ce0b65e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

randomWithChances.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function getRandom(n) {
2+
const e = Array.from(n);
3+
e.reduce((n, e, r, t) => t[r] = n + e.chance, 0);
4+
return n[e.findIndex(n => n > Math.random() * e[e.length - 1])];
5+
}
6+
7+
/* Usage Examples: */
8+
getRandom([
9+
{ rarity: 'Common', chance: 60 },
10+
{ rarity: 'Uncommon', chance: 25 },
11+
{ rarity: 'Rare', chance: 10 },
12+
{ rarity: 'Very Rare', chance: 5 }
13+
]);
14+
15+
getRandom([
16+
{ side: 'VSC', chance: 50 },
17+
{ side: 'WebStorm', chance: 50 }
18+
]);

0 commit comments

Comments
 (0)