Yes. You can generate a random value in a variable and let the NPC tell the player the value of that variable using \v[VARIABLE_ID] in a message. When you let the player enter a code, compare the variable you used for the code with the one containing randomly generated one.