Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to generate a random number in sequence include current date + six digit sequence number.

for example 20141003 + 000001 and the last number will change will the every date...!

example :

20141003 + 000001, 20141003 + 000002........
20141004 + 000001 ,20141004 + 000002........
20141005 + 000001 ,20141005 + 000002........


how can i implement it in c# ?
Posted
Comments
Sergey Alexandrovich Kryukov 3-Sep-14 2:02am    
Why?
—SA
Naveen Singh 3-Sep-14 2:04am    
what why i have to generate a sequence no for that purpose i need that
Sergey Alexandrovich Kryukov 3-Sep-14 2:57am    
Your comment is neither a comprehensible statement nor correct clause.
What makes you think that you need this sequence? What's the purpose: unique ID, something else...
—SA
Naveen Singh 3-Sep-14 3:32am    
somewhat a unique transaction
Sergey Alexandrovich Kryukov 3-Sep-14 3:46am    
You could simply use, say, System.UInt64 number, persisting the last value somewhere....
—SA

We can't specifically answer that: it depends on how you are storing information.
If you are using a database, then there are one set of ways, if these are for filenames, then there is another - and so on.
Because you was a daily sequence number, you need to store either the previous value or a "date" and "sequence number" separately - and how you generate the "next" value depends on how your whole system is used. If this is a single user system, then it's a trivial task - but for a multiuser environment it's a lot more complex as you have to prevent two separate processes from generating the same value.

But some general rules apply anyway:
1) Never "Precalculate" the value: only ever assign it when you commit a record, or file or whatever. Do not try to say "my next number will be 'dddddnnnnn' so I'll use that" - it will always give you grief at some point
2) If in doubt, assume that you will be using this as multiuser. It's more complex, but a lot easier to implement and test to start with than to add later.
3) If possible, take advantages of facilities provided by the environment: databases are (relatively) used to this and can make things easier. So if you are using a database, don't rely on a "manual system" implemented electronically.
 
Share this answer
 
Comments
Naveen Singh 3-Sep-14 2:00am    
i want to use databse method for this purpose
Technically that aren't a random numbers. You may easily generate them if you store both the date and sequence number of the last generated one in a persistent storage (e.g. file, database). Then
  • if date changed, use the new date and reset the sequence number.

on the other hand
  • if the date is the same then use such date and increment the sequence number.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Sep-14 2:58am    
First clause is more than enough to get my 5. :-)
—SA
CPallini 3-Sep-14 3:25am    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900