JavaScript Library that handle some random operations on numbers and arrays 🔥🔥🔥
using yarn
yarn add randnjs
using npm
npm install randnjs
Randnjs offers you some magics methods 🌟 to handle random operations. So now we will show you all these methods. Keep reading... 📖
You have to import the library first before using it. Do like this
import { Random } from 'randnjs'
or
const { Random } = require('randnjs')
- Generate a random number
For doing this, you have to use the generate(min?, max?) method that Random class offers.
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
import { Random } from 'randnjs'
console.log(Random.generate(5, 10)) // Output: 7 for example...
- 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
import { Random } from 'randnjs'
const samples = Random.samples()
console.log(samples) // output: [12345] for example
import { Random } from 'randnjs'
const samples = Random.samples(5)
console.log(samples) // output: [1, 100, 50, 12345, 243] for example
import { Random } from 'randnjs'
const samples = Random.samples(5, 1, 10)
console.log(samples) // output: [1, 10, 3, 6, 2] for example
- 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
import { Random } from 'randnjs'
const numbers = [1, 6, 10, 24, 15]
const randomNumber = Random.choice(numbers)
console.log(randomNumber) // Output: 6 for example
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 🙏...