开发者

SQL Server newID - how is it created?

I would like to use newId to generate random numbers. Usually you would use it just once, but I might be generating up to 10 开发者_如何学Crandom numbers per newId.

Is it random enough?


Usually you would use it just once, but I might be generating up to 10 random numbers per newId. Is it random enough?

It depends on how you extract the numbers from the newid. You cannot treat it as 128 independently random bits.

For example if you use the first 8 bits for generating one random number between 0 and 255, use the next 8 bits to generate another number, etc. then you will see that your numbers will not be uniformly random.

              v

E058D654-35A8-47F2-AE40-1C4EEBBDC549
01461481-ED8D-4B85-90FA-C08621D98DAE
AE861E4E-3469-4BDB-A38B-0031DACC8DAE
AF8905D0-E41B-4300-94F2-33BB45698CD1
003308A6-AE0A-4E20-9F24-047A6955E748
76F9B7ED-79AB-4EB1-B361-8C0AF5177CE3
B8F1CAC0-591D-436B-BB21-FAAD9EECA983
7FBEAEFD-2163-4315-A783-8106909E47D8
85E2FC60-E7B3-400F-B20A-CEFBECAEE4F9
17ED0A03-ADAD-4521-97EE-04815A867B32

              ^
              |
              always 4

You should also try to avoid reusing the same bits to generate different random numbers as your numbers will become related. If in doubt, don't reuse the same number.

Note that there is also a RAND function which you can call. This returns numbers from a uniform distribution.


Yes, it's statistically random. It's simply a GUID.

How do you plan on generating 10 numbers from one seed though? CHECKSUM(NEWID()) is normally how you'd do it for one value, perhaps with modulo and ABS


NewID generates a GUID. It is random enough.


Random enough for what? When you say use it to generate, are you just going to use it to seed a PNRG? I'm not sure it's any better than a timestamp for that. Or are you going to extract bits from the GUID - that's a bad idea.

http://www.random.org/randomness/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜