如何自动生成唯一编号,任何人都可以发送代码 [英] How to auto generate unique numbers,Can anyone send the code

查看:109
本文介绍了如何自动生成唯一编号,任何人都可以发送代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用编码自动生成一个唯一编号,这意味着它看起来像A001,A002或其他任何东西,但是所有编号都是唯一的,

请帮帮我,
感谢任何形式的帮助..

解决方案

尝试:

 Guid g = Guid.NewGuid(); 


http://www.dotnetperls.com/random [


它会给您一些密钥,例如496bffe0.

Guid.NewGuid().ToString().GetHashCode().ToString("x"); 


它提供了类似于649cf2e3


的密钥

I want to generate a UNIQUE numbers automatically with the use of codeing, means it will be looks like A001, A002 or any thing else but all the number will be UNIQUE,

Please help me,
Any kind of help appreciated..

解决方案

Try:

Guid g = Guid.NewGuid();


http://www.dotnetperls.com/random[^]

or

Generating random numbers in C#

Random numbers may be generated in the Microsoft .NET Framework by using the Random class. This class is instantiated using the following C# code:

//Create a new Random class in C#

Random RandomClass = new Random();



This class can subsequently be used to generate random numbers in a number of different data types.
Generating random integers

Once the Random class has been instantiated, a random integer can be obtained by calling the Next method of the Random class:

int RandomNumber = RandomClass.Next();


Using this code sample, the value of the RandomNumber variable will be assigned a random whole number between 1 and 2,147,483,647.

In most programming situations, it is useful to be able to create a random number within a certain size range. In this case, the Next method should be invoked with a different constructor - one that takes two arguments: the minimum value and the maximum value for the random number. For example, the following assigns a value to RandomNumber that is greater or equal to 40 and less than 400:

int RandomNumber = RandomClass.Next(40, 400);



Remember that an ArgumentOutOfRangeException will be raised if the minimum value is larger than the maximum value specified.

It is also possible to use a further constructor to specify just the maximum value of the random integer. The following will return a return a random integer that is greater or equal to 0 and less than 800:

int RandomNumber = RandomClass.Next(800);



As with the previous code sample, an ArgumentOutOfRangeException exception will be raised if the maximum value is smaller than 0.
Generating random floating point numbers

As well as returning random integers, the Random class can also return floating point numbers. The NextDouble method returns a random number as a Double data type. The random number''s value is always greater or equal to 0.0, and less than 1.0:

double RandomNumber = RandomClass.NextDouble();


Generating a random byte array

Finally, the Random class can also be used to populate a byte array with random bytes. This is achieved by calling the NextBytes method of the Random class object.

byte[] ByteArray = new byte[65];
Random.NextBytes(ByteArray);


The array is then populated with bytes of values between 0 and 255.

Note that these random bytes will not be sufficiently random for use in cryptography.


DateTime.Now.ToString().GetHashCode().ToString("x"); 


It will give you some key like 496bffe0.

Guid.NewGuid().ToString().GetHashCode().ToString("x"); 


It gives key something like 649cf2e3


这篇关于如何自动生成唯一编号,任何人都可以发送代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆