1.0.1 • Published 2 years ago

randnjs v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

randnjs

JavaScript Library that handle some random operations on numbers and arrays 🔥🔥🔥

Installation

using yarn

yarn add randnjs

using npm

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

import { Random } from 'randnjs'

or

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.

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

ParametersTypesDefault valueRequiredDescription
minnumber0noMinimum value that can be generated
maxnumber100000000noMaximum 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...
  1. 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

ParametersTypesDefault valueRequiredDescription
lengthnumber1noNumber of elements to generate
minnumber0noMinimum value that can be generated
maxnumber100000000noMaximum 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
  1. 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

ParametersTypesDefault valueRequiredDescription
arrayArray-yesArray 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 🙏...

1.0.1

2 years ago

1.0.0

2 years ago