Skip to content

Commit

Permalink
rewrite random function
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-j committed Mar 4, 2020
1 parent a592adf commit fd9edc7
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ const { spawn } = require('child_process')
const expireDelay = 20
const port = 3001
const maxFileSize = 1024 * 1024 * 400
const keyMin = 1000
const keyMax = 9999

function uniqueRandom (minimum, maximum) {
let previousValue
return function random() {
const number = Math.floor(
(Math.random() * (maximum - minimum + 1)) + minimum
)
previousValue = number === previousValue && minimum !== maximum ? random() : number
return previousValue
}
function random(minimum, maximum) {
return Math.floor((Math.random() * (maximum - minimum + 1)) + minimum)
}

function removeKey (key) {
Expand Down Expand Up @@ -52,7 +47,6 @@ function expireKey (key) {
return timer
}

const random = uniqueRandom(1000, 9999)
const app = new Koa()
app.context.keys = new Map()
const router = new Router()
Expand Down Expand Up @@ -96,7 +90,7 @@ router.get('/generate', async ctx => {
let key = null
let attempts = 0
do {
key = random().toString()
key = random(keyMin, keyMax).toString()
console.log(attempts, ctx.keys.size, key)
if (attempts > ctx.keys.size) {
console.error('Can\'t generate more keys, map is full.')
Expand Down

0 comments on commit fd9edc7

Please sign in to comment.