-5

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

Keith Costa
  • 1,783
  • 11
  • 35
  • 68
  • Um. Your best bet is probably taking the current time in seconds, dividing by 5, and using a hash algorithm like md5sum, then take the md5sum modulo 500000. – Dan Jan 02 '12 at 06:27
  • Why isn't this a real question? – the_drow Jan 02 '12 at 06:31
  • What is the actual problem that you're trying to solve? It sounds like an expiring token, in which case encrypting the current time is a better solution. But without telling us what you're trying to do, nobody can answer "is it OK?". – kdgregory Jan 02 '12 at 15:27

2 Answers2

1

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"

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • ok if my objective can not be possible by random class then tell me what i need to use. i need to generate 6 digit number based on date and time which i can later determine when the number was generated. if u have any idea then please share with me. thanks – Keith Costa Jan 02 '12 at 07:09
  • I don't understand your comment. Just keep an association between existing keys and their data (with creation time). Generate the key as you wish. – Basile Starynkevitch Jan 02 '12 at 07:10
  • Perhaps your question was badly asked. Perhaps you don't need a *random* key, but a key which *hides* some information (the date and time). Use any (simple) cryptographic technique. Don't use any randomness, you need a reproducible and deterministic behavior. – Basile Starynkevitch Jan 02 '12 at 07:21
  • which crypto technique can generate digit if i input current date like DateTime.Now.ToString("yyyyMMddHHmmssffff"). have any idea? – Keith Costa Jan 02 '12 at 08:28
  • Many of them. You need something which is reversible. The choice is up to you (what kind of security & performance do you require?). – Basile Starynkevitch Jan 02 '12 at 08:35
  • I voted to close the question, because I feel it was not well asked... – Basile Starynkevitch Jan 02 '12 at 08:53
0

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);
user703016
  • 37,307
  • 8
  • 87
  • 112
  • ok if my objective can not be possible by random class then tell me what i need to use. i need to generate 6 digit number based on date and time which i can later determine when the number was generated. if u have any idea then please share with me. thanks – Keith Costa Jan 02 '12 at 07:09
  • Use a [timestamp](http://stackoverflow.com/questions/892074/function-that-creates-a-timestamp-in-c-sharp). – user703016 Jan 02 '12 at 07:34
  • logic is not clear to me...will u plzz come with some sample code for timestap – Keith Costa Jan 02 '12 at 07:51