My task for today is to generate a PIN. A four digit pin. It has to be random. Are there limitations to doing it and doing it the way I plan? I understand humans can never create something TRULY random but hey who we have to try.
I am currently generating it in client (JS), then passing to the Model (C#) and this is injected to the DB (Oracle). The only safeguards I can think of so far is either generate from 0001 > onwards to anything less than 10000 so 9999 will be the highest number. Anything that is returned that is between these two numbers as long as it has not been used before in my logic is random - we also know we have to check the db to see if the PIN already exists IF NOT then generate it.
I guess I can:
onGen() {
//generate pin
var pin = characters.charAt(Math.floor(Math.random() * charactersLength));
//check db if pin exists
const exists = checkDB('select pin from pins where pin =' + pin + ';')
//if pin exist generate again
if(exists) pin = characters.charAt(Math.floor(Math.random() * charactersLength));
//also should I put my login of 0001 > and < 10000 in the SELECT or js?
if pin > 0000 return pin
//or this
checkDB('select pin from pins where pin =' + pin + 'AND pin > 0001 and pin < 10000;')
}