added randomString

This commit is contained in:
Rubin Bhandari
2020-10-05 01:24:03 +05:45
committed by GitHub
parent 28a355de59
commit 1752dfc32a

18
snippets/randomString.md Normal file
View File

@ -0,0 +1,18 @@
---
title: randomString
tags: math,random,beginner
---
Returns a random string of the specified length.
- `Math.random()` generate a random number
- `ToString(16)` converts the number into a text string
- Finally, the `substr(2,8)` extracts chracters between the positions 2 and supplied length.
```js
const randomString = (length) => Math.random().toString(20).substr(2, length)
```
```js
randomString(0afad); // '0afad'
```