-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change the random generation method #10124
Change the random generation method #10124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine, but this whole thing seems pretty heavyweight when we could do something like https://sharplab.io/#v2:EYLgHgbALAPgAgJgIwFgBQcAMACOSB0AIgJYCGA5gHYD2AzgC7EDGtA3OunAMy4LYDC2AN7psY3Dzw4AsgApiletgA2AU0rl6ACwCUw0eMMAVAE6kmq/AEFatVSfqy1G7dgA82Lgh3s0hw3AA7NgA4gCuxAAm+AByqgDu4VGyOvhG1ADK9CYK5LIARDH5qRlhwFKymAA0Kuqaur6GAL7oTUA
@@ -17,15 +18,16 @@ internal static class StringUtils | |||
internal static string GenerateRandomString(int length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update doc comment above
@@ -17,15 +18,16 @@ internal static class StringUtils | |||
internal static string GenerateRandomString(int length) | |||
{ | |||
// Base64, 2^6 = 64 | |||
using var rng = RandomNumberGenerator.Create(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a performance advantage in holding one of these in a static (or thread static)? I don't know, but some implementations of such things generate a block and then return it in pieces as requested. Or pull expensive entropy to start up. @vcsjones ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RandomNumberGenerator
was never documented as thread-safe, even though in all practical cases, it is thread-safe when used with RandomNumberGenerator.Create()
. Practically speaking, it would work fine to pull this up to a static, even in .NET Framework.
We've made significant investments in the RandomNumberGenerator for .NET (Core). If you want to avoid the allocation for the modern .NET, you can do something like
byte[] randomBytes;
#if NET
randomBytes = RandomNumberGenerator.GetBytes(randomBytesNeeded);
#else
randomBytes = new byte[randomBytesNeeded];
using var rng = RandomNumberGenerator.Create();
rng.GetBytes(randomBytes);
#endif
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for suggestions.
We're currently using this utility to create name of a build binary log file - so simpler code is preference
We've discouraged using |
AFAIK the individual parts of guids have different distributions (there is a part derived from machine uuid as well I believe - so effectively constant on a single machine) - so the substrings of particular length might not serve the purpose well. |
This is used in one place to disambiguate a filename. I would love to delete the code entirely and use a non-cryptographic semi-random thing in its place. |
Closing the PR: Since the System.Random used for the filename. Thank you for all the comments and reviews |
for what it's worth, if you are x-targeting to .NET Core, you can switch out |
Context
Changing the usage of the Random to RandomNumberGenerator, to align with the codebase