rewrite random function
This commit is contained in:
16
index.js
16
index.js
@@ -12,16 +12,11 @@ const { spawn } = require('child_process')
|
|||||||
const expireDelay = 20
|
const expireDelay = 20
|
||||||
const port = 3001
|
const port = 3001
|
||||||
const maxFileSize = 1024 * 1024 * 400
|
const maxFileSize = 1024 * 1024 * 400
|
||||||
|
const keyMin = 1000
|
||||||
|
const keyMax = 9999
|
||||||
|
|
||||||
function uniqueRandom (minimum, maximum) {
|
function random(minimum, maximum) {
|
||||||
let previousValue
|
return Math.floor((Math.random() * (maximum - minimum + 1)) + minimum)
|
||||||
return function random() {
|
|
||||||
const number = Math.floor(
|
|
||||||
(Math.random() * (maximum - minimum + 1)) + minimum
|
|
||||||
)
|
|
||||||
previousValue = number === previousValue && minimum !== maximum ? random() : number
|
|
||||||
return previousValue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeKey (key) {
|
function removeKey (key) {
|
||||||
@@ -52,7 +47,6 @@ function expireKey (key) {
|
|||||||
return timer
|
return timer
|
||||||
}
|
}
|
||||||
|
|
||||||
const random = uniqueRandom(1000, 9999)
|
|
||||||
const app = new Koa()
|
const app = new Koa()
|
||||||
app.context.keys = new Map()
|
app.context.keys = new Map()
|
||||||
const router = new Router()
|
const router = new Router()
|
||||||
@@ -96,7 +90,7 @@ router.get('/generate', async ctx => {
|
|||||||
let key = null
|
let key = null
|
||||||
let attempts = 0
|
let attempts = 0
|
||||||
do {
|
do {
|
||||||
key = random().toString()
|
key = random(keyMin, keyMax).toString()
|
||||||
console.log(attempts, ctx.keys.size, key)
|
console.log(attempts, ctx.keys.size, key)
|
||||||
if (attempts > ctx.keys.size) {
|
if (attempts > ctx.keys.size) {
|
||||||
console.error('Can\'t generate more keys, map is full.')
|
console.error('Can\'t generate more keys, map is full.')
|
||||||
|
|||||||
Reference in New Issue
Block a user