https://github.com/letieu/chance-percent
https://github.com/letieu/chance-percent
change percent random-generation
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/letieu/chance-percent
- Owner: letieu
- Created: 2022-03-10T05:42:18.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-03T08:01:50.000Z (almost 4 years ago)
- Last Synced: 2025-03-01T14:06:52.104Z (over 1 year ago)
- Topics: change, percent, random-generation
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/chance-percent
- Size: 4.07 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Get random item base on percentage
## Simple usage
```javascript
import { random } from 'chance-percent';
const options = [
{value: 1, percentage: 10},
{value: 3, percentage: 60},
{value: 2, percentage: 30},
]
const value = random(options); // return 1, 2, 3
```
## Typescript usage
```typescript
import { random, ChanceOption } from 'chance-percent';
interface User {
age: number;
name: string;
}
const users = [{ age: 20, name: 'bob' }, { age: 40, name: 'jonh' }]
const options: ChanceOption[] = [
{value: users[1], percentage: 40},
{value: users[2], percentage: 60},
]
const winner: User = random(options);
```