gpt4 book ai didi

c# - 如何播种 GUID 生成?

转载 作者:太空狗 更新时间:2023-10-29 18:03:26 34 4
gpt4 key购买 nike

在 .NET 中编写函数代码以生成基于种子的 GUID 以便我对其唯一性更有信心的最简单方法是什么?

string GenerateSeededGuid(int seed) { /* code here */ }

理想情况下,种子来自 CryptGenRandom其随机数生成描述如下:

The data produced by this function is cryptographically random. It is far more random than the data generated by the typical random number generator such as the one shipped with your C compiler.

This function is often used to generate random initialization vectors and salt values.

Software random number generators work in fundamentally the same way. They start with a random number, known as the seed, and then use an algorithm to generate a pseudo-random sequence of bits based on it. The most difficult part of this process is to get a seed that is truly random. This is usually based on user input latency, or the jitter from one or more hardware components.

With Microsoft CSPs, CryptGenRandom uses the same random number generator used by other security components. This allows numerous processes to contribute to a system-wide seed. CryptoAPI stores an intermediate random seed with every user. To form the seed for the random number generator, a calling application supplies bits it might have—for instance, mouse or keyboard timing input—that are then combined with both the stored seed and various system data and user data such as the process ID and thread ID, the system clock, the system time, the system counter, memory status, free disk clusters, the hashed user environment block. This result is used to seed the pseudorandom number generator (PRNG). [...] If an application has access to a good random source, it can fill the pbBuffer buffer with some random data before calling CryptGenRandom. The CSP then uses this data to further randomize its internal seed. It is acceptable to omit the step of initializing the pbBuffer buffer before calling CryptGenRandom.

最佳答案

tldr; 使用 Guid.NewGuid 而不是试图发明另一种“更随机”的方法。 (我能想到从种子创建 UUIDvX 的唯一原因是需要可预测、可重置的序列。但是,GUID 可能也不是最好的方法2 .)

根据有限范围的定义 - 128 位 减去 6 个版本控制位,所以 122 bits of uniqueness for v4 - 只有这么多(albeit supremely huge number! astronomically big!)“唯一”标识符。

由于 Pigeonhole Principle只有这么多的鸽子洞。如果鸽子最终继续繁殖,每只鸽子将没有足够的洞。由于 Birthday Paradox ,假设完全随机,两只鸽子会在它们都被填满之前尝试争夺同一个鸽子洞。因为没有 Master Pigeonhole List1 这无法避免。此外,并非所有动物都是鸽子3

同时 there are no guarantees as to which GUID generator will be used , .NET 使用底层操作系统调用,它是一个 GUIDv4(又名 Random UUID)生成器 since Windows 2k .据我所知——或者说在意,真的——这是一个很好的随机数,因为它可以达到这样的目的。它已经过 十多年 的严格审查,并且没有被替换。


来自维基百科:

.. only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs.

1 虽然仍然有一组有限的 Pigeonholes,UUIDv1 (又名 MAC UUID)——假设唯一的时空——保证生成确定性唯一的数字(在给定机器上每秒生成一些“相对较小”的理论最大 UUID 数)。不同种族的鸽子生活在不同的平行维度 - 太棒了!

2 Twitter uses Snowflakes在其自己的分布式唯一 ID 方案中的平行维度。

3 兔子喜欢住在洞穴里,而不是鸽子洞里。 GUID 的使用 也充当隐式并行分区。只有当重复的 GUID 用于同一目的时,才会出现与冲突相关的问题。想想有多少重复的自增数据库主键!

关于c# - 如何播种 GUID 生成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188280/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com