简体   繁体   English

哪种方法更好地调用此id生成方法?

[英]which way is better to call this id generation method?

private static Int64 NextInt64(Random rnd) 
{     
    var buffer = new byte[sizeof(Int64)];     
    rnd.NextBytes(buffer);     
    return BitConverter.ToInt64(buffer, 0); 
} 

above method is from this thread: Generate random values in C# 上面的方法来自这个线程: 在C#中生成随机值

It will be used in an abstract event class to generate unique eventId. 在抽象事件类中将使用它来生成唯一的eventId。 that class will be used frequently because we need to send lots of event. 该类将被频繁使用,因为我们需要发送大量事件。 When I call above method in the event class constructor which option is better from the performance and unique value point of view: 当我在事件类构造函数中调用上述方法时,从性能和唯一值的角度来看,哪个选项更好:

  1. create a Random object with new and pass it into above method each time; new创建一个Random对象,并每次将其传递给上述方法;
  2. create a static Random object at very beginning and use it to call above method repeatedly when needed. 首先创建一个静态Random对象,并在需要时使用它重复调用上述方法。

You're definitely better off creating one Random instance and using it multiple times. 绝对最好创建一个Random实例并多次使用它。 You will achieve better 'randomness' that way. 这样您将获得更好的“随机性”。

If you check out microsoft's documentation here you'll see that two Random instances produce the exact same numbers. 如果您在此处查看Microsoft的文档将会看到两个随机实例产生的数字完全相同。

Another way to get 'better' random numbers is to provide a seed. 获得“更好”随机数的另一种方法是提供种子。 Some pass the current time as an int to get a very unique seed. 有些人将当前时间作为整数来获取非常独特的种子。

  • You need option 2) anyway, otherwise al your random's will be the same. 无论如何,您都需要选择2),否则您的随机数将相同。 See this . 看到这个
  • you can turn this method into an extenison, 您可以将这种方法变成一种手段,

Like this: 像这样:

 public static class Helpers
 {
   public static Int64 NextInt64(this Random rnd) { ... }
 }

But ... 但是...

It will be used in an abstract event class to generate unique eventId. 在抽象事件类中将使用它来生成唯一的eventId。

No, this will not generate unique Id's . 不,这不会生成唯一的ID。 Be prepared for collisions. 为碰撞做好准备。

Use a simple counter (nextId) or GUids 使用简单的计数器(nextId)或GUids

System Guid structure is exactly for this purpose. System Guid结构正是用于此目的。 Rather than trying to pass on a seemingly randomized object, pass a guid structure as an argument. 与其尝试传递看似随机的对象,不如传递guid结构作为参数。

private static Guid  NextIntGuid() 
{
  return Guid.NewGuid();
}

As the name implies, it is already for representing a globally unique identifiers. 顾名思义,它已经用于表示全局唯一标识符。 Also if you insist on using random numbers, use RandomNumberGenerator inside the System.Security.Cryptography namespace. 另外,如果您坚持使用随机数,请在System.Security.Cryptography命名空间内使用RandomNumberGenerator If sufficiently large, your random number will almost unique: 如果足够大,您的随机数将几乎是唯一的:

byte[] random = new Byte[128];

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(random); // The array is now filled with cryptographically strong random bytes.

1 will cause issues with randomness unless you give it a unique seed each time. 除非每次给它一个唯一的种子,否则1会导致随机性问题。 Not to mention putting extra pressure on the garbage collector. 更不用说对垃圾收集器施加额外的压力。

2 is a better option - the Random class is designed to generate a sequence of random numbers, not just one. 2是更好的选择-Random类旨在生成一系列随机数,而不仅仅是一个。 So while it's better to have a single instance of Random, it doesn't necessarily need to be accessible with a static property, it could be exposed in any number of ways. 因此,虽然最好有一个Random实例,但不一定需要使用static属性即可访问它,它可以通过多种方式公开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM