Question: what logic i can use to generate 6 digit random number based on current date and time. suppose i want to generate a 6 digit random number which will be valid up to 5 second. after 5 second if anyone input that random number into system then system will say it is expire. one thing i need to mention that there will be no database interaction. i don't want to generate random number and store it in database table. 5 second validity logic will be embedded in generated random number as a result i can validate it later that whether it is generate before 5 sec or not. 

i asked this question in another forum and they gave me code to generate 6 digit random number based on current date and time like

```
var random = new Random(System.DateTime.Now.Millisecond);
int randomNumber = random.Next(1, 500000);

```

is it ok? because i am not advance developer.if i can generate 6 digit random number using the above logic then later how can i determine programatically that the number was generate before 5 sec or not. basically i need two routine one for generating 6 digit random number based on current date & time and another routine will check that generated number was generated before 5 sec or not.

please guide me with concept and code. thanks

Answer: 
If it is the same process which stays during these five seconds, you could just store the key inside an hashtable, and to validate it check that it is inside the hashtable. Also keep in the hashtable the creation time of the key, and periodically clean that hashtable (removing obsolete keys and associated data).

Of course you'll need to generate that key either "randomly" or "cryptographically"
You can't reverse the process of random number generation to determine *when* the number were generated, particularly if you're seeding `Random` with milliseconds.

Getting a 6-digit random number:

```
int randomNumber = random.Next(0, 1000000);

```