mohammadKarimi / SwiftLink

SwiftLink is a modern URL shortener with Asp.net Core 8
MIT License
105 stars 26 forks source link

Generating random numbers from multiple threads #3

Closed Kamyab7 closed 8 months ago

Kamyab7 commented 8 months ago

Hey @mohammadKarimi

In HashBasedShortCodeGenerator you are trying to use the lock keyword to make it thread-safe and it's totally fine but I want to suggest Random.Shared.

As I know, recording to this article building-a-thread-safe-random-implementation written by @andrewlock, The humble Random class has been around since .NET Framework 1.0, but an important thing to bear in mind is that it's not thread-safe by default. I'm going to describe. If you're generating random numbers in .NET 6+, you don't need to control the "seed" value, and you need thread-safe access, then you can use Random.Shared.