Release the latest generator

This commit is contained in:
fechin committed 2022-11-16 14:10:09 +08:00
1 parent b520119d38
commit 15c93e54ee
484 files changed
+4969 -856

No files matched your search

@@ -0,0 +1,22 @@
---
title: How to generate a random boolean in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random boolean only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomBoolean = () => Math.random() >= 0.5;
```
@@ -0,0 +1,22 @@
---
title: How to generate a random floating point number in given range in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random floating point number in given range only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomFloat = (min, max) => Math.random() * (max - min) + min;
```
@@ -0,0 +1,28 @@
---
title: How to generate a random hex color in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random hex color only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomColor = () => `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`;
```
### Or
```js {.wrap}
const randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`;
```
@@ -0,0 +1,22 @@
---
title: How to generate a random integer in given range in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random integer in given range only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomInteger = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
```
@@ -0,0 +1,28 @@
---
title: How to generate a random ip address in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random ip address only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomIp = () => Array(4).fill(0).map((_, i) => Math.floor(Math.random() * 255) + (i === 0 ? 1 : 0)).join('.');
```
### Example
```js {.wrap}
randomIp(); // 175.89.174.131
```
@@ -0,0 +1,22 @@
---
title: How to generate a random sign in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random sign only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomSign = () => Math.random() >= 0.5 ? 1 : -1;
```
@@ -0,0 +1,28 @@
---
title: How to generate a random string from given characters in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random string from given characters only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const generateString = (length, chars) => Array(length).fill('').map((v) => chars[Math.floor(Math.random() * chars.length)]).join('');
```
### Example
```js {.wrap}
generateString(10, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
```
@@ -0,0 +1,22 @@
---
title: How to generate a random string using node crypto module in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random string using node crypto module only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomStr = () => require('crypto').randomBytes(32).toString('hex');
```
@@ -0,0 +1,22 @@
---
title: How to generate a random string with given length in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random string with given length only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const generateString = length => Array(length).fill('').map((v) => Math.random().toString(36).charAt(2)).join('');
```
@@ -0,0 +1,22 @@
---
title: How to generate a random uuid in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate a random uuid only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const uuid = (a) => a ? (a ^ Math.random() * 16 >> a / 4).toString(16) : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid);
```
@@ -0,0 +1,28 @@
---
title: How to generate an array of random integers in a given range in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to generate an array of random integers in a given range only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomArrayInRange = (min, max, n) => Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min);
```
### Example
```js {.wrap}
randomArrayInRange(1, 100, 10); // [11, 82, 41, 35, 76, 83, 43, 15, 60, 54]
```
@@ -0,0 +1,30 @@
---
title: How to get a random item and remove it from an array in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to get a random item and remove it from an array only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomItem = arr => arr.splice((Math.random() * arr.length) | 0, 1);
```
### Example
```js {.wrap}
const arr = [1, 3, 5, 7, 9];
randomItem(arr); // 7
// arr = [1, 3, 5, 9]
```
@@ -0,0 +1,22 @@
---
title: How to get a random item from an array in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to get a random item from an array only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomItem = arr => arr[(Math.random() * arr.length) | 0];
```
@@ -0,0 +1,29 @@
---
title: How to get random items of an array in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to get random items of an array only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomItems = (arr, count) => arr.concat().reduce((p, _, __, arr) => (p[0] < count) ? [p[0] + 1, p[1].concat(arr.splice(Math.random() * arr.length | 0, 1))] : p, [0, []])[1];
```
### Example
```js {.wrap}
randomItems([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3); // [4, 8, 5]
randomItems(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], 4); // ['e', 'c', 'h', 'j']
```
@@ -0,0 +1,73 @@
---
title: How to pick a random property of an object in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to pick a random property of an object only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomProp = obj => Object.keys(obj)[(Math.random() * Object.keys(obj).length) | 0];
```
### Example
```js {.wrap}
const colors = {
aqua: '#00ffff',
azure: '#f0ffff',
beige: '#f5f5dc',
black: '#000000',
blue: '#0000ff',
brown: '#a52a2a',
cyan: '#00ffff',
darkblue: '#00008b',
darkcyan: '#008b8b',
darkgrey: '#a9a9a9',
darkgreen: '#006400',
darkkhaki: '#bdb76b',
darkmagenta: '#8b008b',
darkolivegreen: '#556b2f',
darkorange: '#ff8c00',
darkorchid: '#9932cc',
darkred: '#8b0000',
darksalmon: '#e9967a',
darkviolet: '#9400d3',
fuchsia: '#ff00ff',
gold: '#ffd700',
green: '#008000',
indigo: '#4b0082',
khaki: '#f0e68c',
lightblue: '#add8e6',
lightcyan: '#e0ffff',
lightgreen: '#90ee90',
lightgrey: '#d3d3d3',
lightpink: '#ffb6c1',
lightyellow: '#ffffe0',
lime: '#00ff00',
magenta: '#ff00ff',
maroon: '#800000',
navy: '#000080',
olive: '#808000',
orange: '#ffa500',
pink: '#ffc0cb',
purple: '#800080',
violet: '#800080',
red: '#ff0000',
silver: '#c0c0c0',
white: '#ffffff',
yellow: '#ffff00',
};
randomProp(colors); // 'red'
```
@@ -0,0 +1,34 @@
---
title: How to pick random lines from a text document in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Random
layout: note
---
In this Article we will go through how to pick random lines from a text document only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const randomLines = (str, count) => str.split(/\r?\n/).reduce((p, _, __, arr) => (p[0] < count) ? [p[0] + 1, p[1].concat(arr.splice(Math.random() * arr.length | 0, 1))] : p, [0, []])[1];
```
### Example
```js {.wrap}
randomLines(`one
two
three
four
five`, 2);
// ['one', 'four']
```