https://github.com/dilane3/randnjs
JavaScript Library that handle some random operation on numbers and arrays
https://github.com/dilane3/randnjs
choice generator number random
Last synced: about 1 month ago
JSON representation
JavaScript Library that handle some random operation on numbers and arrays
- Host: GitHub
- URL: https://github.com/dilane3/randnjs
- Owner: dilane3
- Created: 2022-05-15T23:09:26.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-05-16T17:15:29.000Z (about 4 years ago)
- Last Synced: 2025-11-12T06:03:29.351Z (8 months ago)
- Topics: choice, generator, number, random
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# randnjs
JavaScript Library that handle some random operations on numbers and arrays 🔥🔥🔥
## Installation
using **yarn**
```bash
yarn add randnjs
```
using **npm**
```bash
npm install randnjs
```
## Usage
**Randnjs** offers you some magics methods 🌟 to handle random operations.
So now we will show you all these methods. Keep reading... 📖
### First step: Importation
You have to import the library first before using it. Do like this
```javascript
import { Random } from 'randnjs'
```
or
```javascript
const { Random } = require('randnjs')
```
### Second step: Use methods
1. **Generate a random number**
For doing this, you have to use the **generate(min?, max?)** method that **Random** class offers.
```javascript
import { Random } from 'randnjs'
console.log(Random.generate()) // Output: 100 for example...
```
This method can take two optional parameters (min and max values)
**Some parameters**
|Parameters |Types |Default value |Required |Description |
|--- |--- |---- |--- |--- |
|min |number |0 |no |Minimum value that can be generated |
|max |number |100000000 |no |Maximum value that can be generated |
So now you can specify the min and max value
```javascript
import { Random } from 'randnjs'
console.log(Random.generate(5, 10)) // Output: 7 for example...
```
2. **Generate a list of numbers**
To generate a list of numbers, you have to use the **samples(length?, min?, max?)** method on the **Random** class.
It takes three optional parameters
**Some parameters**
|Parameters |Types |Default value |Required |Description |
|--- |--- |---- |--- |--- |
|length |number |1 |no |Number of elements to generate |
|min |number |0 |no |Minimum value that can be generated |
|max |number |100000000 |no |Maximum value that can be generated |
**Examples**
```javascript
import { Random } from 'randnjs'
const samples = Random.samples()
console.log(samples) // output: [12345] for example
```
```javascript
import { Random } from 'randnjs'
const samples = Random.samples(5)
console.log(samples) // output: [1, 100, 50, 12345, 243] for example
```
```javascript
import { Random } from 'randnjs'
const samples = Random.samples(5, 1, 10)
console.log(samples) // output: [1, 10, 3, 6, 2] for example
```
3. **Choose one element from a list of elements**
To select randomly one element from a list of element, you have to use the **choice(array)** method that the **Random** class offers.
It takes one required parameter which is a non empty array of elements. Elements here can be numbers, strings, objects and so on...
**Some parameters**
|Parameters |Types |Default value |Required |Description |
|--- |--- |---- |--- |--- |
|array |Array |- |yes |Array of elements where we extract one element|
**Examples**
```javascript
import { Random } from 'randnjs'
const numbers = [1, 6, 10, 24, 15]
const randomNumber = Random.choice(numbers)
console.log(randomNumber) // Output: 6 for example
```
```javascript
import { Random } from 'randnjs'
const fruits = ['apple', 'banana', 'orange', 'pineapple', 'watermelon']
const randomFruit = Random.choice(fruits)
console.log(randomFruit) // Output: orange for example
```
That's all.
Thank you 🙏...