Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const padToFour = number => number <= 9999 ? ('000' + number).slice(-4) : number
const fs = require('fs')
/**
* Generates an array of numbers between 0 - 9999 with leading zeros and returns
* it.
*
* @returns {number[]} data
*/
const getData = () => {
let data = []
let x = 0
while (x < 10000) {
data.push(padToFour(x))
x++
}
return data
}
/**
* Shuffle the provided array using the Fisher-Yates Shuffle.
*
* @param {number[]} array
*
* @returns {number[]}
*/
const shuffle = array => {
let currentIndex = array.length
let temporaryValue
let randomIndex
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array
}
const data = getData()
const shuffled = shuffle(data)
fs.writeFile('Output.txt', shuffled.join('\n'), err => {
if (err) console.error(err)
else console.log(`Data saved to file.`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.