Skip to content

Commit

Permalink
Improved RandomHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCiliaVincenti committed Aug 15, 2024
1 parent 829d781 commit 6f2b714
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
5 changes: 4 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageVersion Include="AWSSDK.SecurityToken" Version="3.7.300.2" />
<PackageVersion Include="Azure.Messaging.ServiceBus" Version="7.17.0" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageVersion Include="Backport.System.Threading.Lock" Version="1.1.1" />
<PackageVersion Include="Blazorise" Version="1.5.2" />
<PackageVersion Include="Blazorise.Components" Version="1.5.2" />
<PackageVersion Include="Blazorise.DataGrid" Version="1.5.2" />
Expand Down Expand Up @@ -47,6 +48,7 @@
<PackageVersion Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageVersion Include="LdapForNet" Version="2.7.15" />
<PackageVersion Include="LibGit2Sharp" Version="0.28.0" />
<PackageVersion Include="ListShuffle" Version="2.0.0" />
<PackageVersion Include="Magick.NET-Q16-AnyCPU" Version="13.4.0" />
<PackageVersion Include="MailKit" Version="4.7.1.1" />
<PackageVersion Include="Markdig.Signed" Version="0.37.0" />
Expand Down Expand Up @@ -165,6 +167,7 @@
<PackageVersion Include="System.Text.Encodings.Web" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
<PackageVersion Include="ThreadSafeRandomizer" Version="1.1.4" />
<PackageVersion Include="TimeZoneConverter" Version="6.1.0" />
<PackageVersion Include="Unidecode.NET" Version="2.1.0" />
<PackageVersion Include="xunit" Version="2.6.1" />
Expand All @@ -174,4 +177,4 @@
<PackageVersion Include="ConfigureAwait.Fody" Version="3.3.2" />
<PackageVersion Include="Fody" Version="6.8.0" />
</ItemGroup>
</Project>
</Project>
4 changes: 3 additions & 1 deletion framework/src/Volo.Abp.Core/Volo.Abp.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\..\configureawait.props" />
<Import Project="..\..\..\common.props" />
Expand All @@ -16,6 +16,7 @@
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ListShuffle" />
<PackageReference Include="System.Collections.Immutable" />
<PackageReference Include="Microsoft.Extensions.Localization" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
Expand All @@ -32,6 +33,7 @@
<PackageReference Include="System.Linq.Queryable" />
<PackageReference Include="JetBrains.Annotations" />
<PackageReference Include="Nito.AsyncEx.Context" />
<PackageReference Include="ThreadSafeRandomizer" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETStandard' And $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '2.1')) ">
<PackageReference Include="System.ComponentModel.Annotations" />
Expand Down
33 changes: 8 additions & 25 deletions framework/src/Volo.Abp.Core/Volo/Abp/RandomHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using ListShuffle;
using ThreadSafeRandomizer;

namespace Volo.Abp;

Expand All @@ -11,8 +12,6 @@ namespace Volo.Abp;
/// </summary>
public static class RandomHelper
{
private static readonly Random Rnd = new Random();

/// <summary>
/// Returns a random number within a specified range.
/// </summary>
Expand All @@ -25,10 +24,7 @@ public static class RandomHelper
/// </returns>
public static int GetRandom(int minValue, int maxValue)
{
lock (Rnd)
{
return Rnd.Next(minValue, maxValue);
}
return ThreadSafeRandom.Instance.Next(minValue, maxValue);
}

/// <summary>
Expand All @@ -42,10 +38,7 @@ public static int GetRandom(int minValue, int maxValue)
/// </returns>
public static int GetRandom(int maxValue)
{
lock (Rnd)
{
return Rnd.Next(maxValue);
}
return ThreadSafeRandom.Instance.Next(maxValue);
}

/// <summary>
Expand All @@ -54,10 +47,7 @@ public static int GetRandom(int maxValue)
/// <returns>A 32-bit signed integer greater than or equal to zero and less than <see cref="int.MaxValue"/>.</returns>
public static int GetRandom()
{
lock (Rnd)
{
return Rnd.Next();
}
return ThreadSafeRandom.Instance.Next();
}

/// <summary>
Expand Down Expand Up @@ -93,16 +83,9 @@ public static List<T> GenerateRandomizedList<T>([NotNull] IEnumerable<T> items)
{
Check.NotNull(items, nameof(items));

var currentList = new List<T>(items);
var randomList = new List<T>();

while (currentList.Any())
{
var randomIndex = RandomHelper.GetRandom(0, currentList.Count);
randomList.Add(currentList[randomIndex]);
currentList.RemoveAt(randomIndex);
}
var list = new List<T>(items);
list.Shuffle();

return randomList;
return list;
}
}

0 comments on commit 6f2b714

Please sign in to comment.