我想使用当前日期生成随机数? [英] i want to generate random number using current date ?
本文介绍了我想使用当前日期生成随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按顺序生成一个随机数,包括当前日期+六位数序列号。
例如20141003 + 000001,最后一个数字将改变每个日期......!
例如:
20141003 + 000001,20141003 + 000002 ... .....
20141004 + 000001,20141004 + 000002 ........
20141005 + 000001,20141005 + 000002 ....... 。
如何在c#中实现它?
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# ?
推荐答案
我们无法具体回答:这取决于您如何存储信息。
如果您使用的是数据库,那么有一套方法,如果这些是文件名,那么还有另外一种 - 依此类推。
因为你是一个每日序列号,您需要分别存储前一个值或日期和序列号 - 以及如何生成下一个值取决于整个系统的使用方式。如果这是一个单一的用户系统,那么这是一项微不足道的任务 - 但对于多用户环境来说,它要复杂得多,因为你必须阻止两个独立的进程生成相同的值。
但是仍然适用一些通用规则:
1)永远不要预先计算值:只有在提交记录,文件或其他内容时才分配它。不要试图说我的下一个号码将是'dddddnnnnn'所以我会使用它 - 它会在某些时候给你带来悲伤
2)如果有疑问,假设你会使用它作为多用户。它更复杂,但实现和测试起来要比以后添加容易得多。
3)如果可能,利用环境提供的设施:数据库(相对)用于此并且可以使事情变得更容易因此,如果您使用的是数据库,请不要依赖于以电子方式实现的手动系统。
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.
从技术上讲,这不是随机数。如果将最后生成的date
和序列号
存储在持久存储中,则可以轻松生成它们(例如文件,数据库)。然后
- 如果日期改变,请使用新日期并重置序列号。
Technically that aren't a random numbers. You may easily generate them if you store both thedate
andsequence 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.
- 如果日期相同,则使用此日期并增加序列号。
这篇关于我想使用当前日期生成随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文