Skip to content

Instantly share code, notes, and snippets.

@branneman
Created December 6, 2023 15:15
Show Gist options
  • Save branneman/44a8f05eaa8ab84b1c891a79bf50e757 to your computer and use it in GitHub Desktop.
Save branneman/44a8f05eaa8ab84b1c891a79bf50e757 to your computer and use it in GitHub Desktop.
Random person selector
const allPeople = ['Aisha', 'Amina', 'Anya', 'Carlos', 'Chen', 'Liam', 'Nia', 'Oliver', 'Raj', 'Yuki' ]
const peopleAlreadySpeaking = ['Anya', 'Raj']
const numberOfPeople = 2
// Fisher–Yates shuffle
function shuffle(list) {
const xs = list.slice()
let currentIndex = xs.length
let randomIndex
// While there remain elements to shuffle
while (currentIndex > 0) {
// Pick a remaining element
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex--
// And swap it with the current element
;[xs[currentIndex], xs[randomIndex]] = [xs[randomIndex], xs[currentIndex]]
}
return xs
}
const eligiblePeople = allPeople.filter((person) => !peopleAlreadySpeaking.includes(person))
const chosenPeople = shuffle(eligiblePeople).slice(0, numberOfPeople)
console.log('------')
console.log(`Randomly chosen: ${chosenPeople.join(', ')}`)
console.log(`Generated on ${new Date()}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment