Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite0fe9d9

Browse files
Use Random.Shared
Use the new .NET 6 Random.Shared static property where possible.Seedotnet/runtime#50297.
1 parent2afcac2 commite0fe9d9

File tree

26 files changed

+56
-76
lines changed

26 files changed

+56
-76
lines changed

‎src/Components/Samples/BlazorServerApp/Data/WeatherForecastService.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ public class WeatherForecastService
1313

1414
publicTask<WeatherForecast[]>GetForecastAsync(DateTimestartDate)
1515
{
16-
varrng=newRandom();
1716
returnTask.FromResult(Enumerable.Range(1,5).Select(index=>newWeatherForecast
1817
{
1918
Date=startDate.AddDays(index),
20-
TemperatureC=rng.Next(-20,55),
21-
Summary=Summaries[rng.Next(Summaries.Length)]
19+
TemperatureC=Random.Shared.Next(-20,55),
20+
Summary=Summaries[Random.Shared.Next(Summaries.Length)]
2221
}).ToArray());
2322
}
2423
}

‎src/Components/WebAssembly/Samples/HostedBlazorWebassemblyApp/Server/Data/WeatherForecastService.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ public class WeatherForecastService : IWeatherForecastService
1515

1616
publicTask<WeatherForecast[]>GetForecastAsync(DateTimestartDate)
1717
{
18-
varrng=newRandom();
1918
returnTask.FromResult(Enumerable.Range(1,5).Select(index=>newWeatherForecast
2019
{
2120
Date=startDate.AddDays(index),
22-
TemperatureC=rng.Next(-20,55),
23-
Summary=Summaries[rng.Next(Summaries.Length)]
21+
TemperatureC=Random.Shared.Next(-20,55),
22+
Summary=Summaries[Random.Shared.Next(Summaries.Length)]
2423
}).ToArray());
2524
}
2625
}

‎src/Components/WebAssembly/testassets/Wasm.Authentication.Server/Controllers/WeatherForecastController.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger)
2828
[HttpGet]
2929
publicIEnumerable<WeatherForecast>Get()
3030
{
31-
varrng=newRandom();
3231
returnEnumerable.Range(1,5).Select(index=>newWeatherForecast
3332
{
3433
Date=DateTime.Now.AddDays(index),
35-
TemperatureC=rng.Next(-20,55),
36-
Summary=Summaries[rng.Next(Summaries.Length)]
34+
TemperatureC=Random.Shared.Next(-20,55),
35+
Summary=Summaries[Random.Shared.Next(Summaries.Length)]
3736
})
3837
.ToArray();
3938
}

‎src/Components/benchmarkapps/BlazingPizza.Server/Pages/Map.razor‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
@functions{
1111
privateintlocX;
1212
privateintlocY;
13-
privateRandomrandom=newRandom();
1413
privateTimertimer;
1514
privatestringlocString;
1615

1716
protectedoverridevoidOnInitialized()
1817
{
1918
timer=newTimer(_=>
2019
{
21-
locX=random.Next(1000);
22-
locY=random.Next(1000);
20+
locX=Random.Shared.Next(1000);
21+
locY=Random.Shared.Next(1000);
2322
locString=$"{locX},{locY}";
2423

2524
InvokeAsync(()=>StateHasChanged());

‎src/Components/benchmarkapps/Wasm.Performance/TestApp/Pages/TimerComponent.razor‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
@code
99
{
10-
Randomrandom=newRandom();
1110
Timertimer;
1211
intred=128;
1312
intgreen=128;
@@ -22,9 +21,9 @@
2221
{
2322
InvokeAsync(()=>
2423
{
25-
red=random.Next(0,256);
26-
green=random.Next(0,256);
27-
blue=random.Next(0,256);
24+
red=Random.Shared.Next(0,256);
25+
green=Random.Shared.Next(0,256);
26+
blue=Random.Shared.Next(0,256);
2827
StateHasChanged();
2928
BenchmarkEvent.Send(JSRuntime,"Finished updating color");
3029
});

‎src/Components/test/testassets/BasicTestApp/ReorderingFocusComponent.razor‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
</ul>
3232

3333
@code{
34-
Randomrng=newRandom();
3534
TodoItem[]todoItems=new[]
3635
{
3736
newTodoItem {Id=1,Text="First" },
@@ -43,7 +42,7 @@
4342

4443
voidShuffle()
4544
{
46-
todoItems=todoItems.OrderBy(x=>rng.Next()).ToArray();
45+
todoItems=todoItems.OrderBy(x=>Random.Shared.Next()).ToArray();
4746
}
4847

4948
classTodoItem

‎src/DataProtection/DataProtection/src/KeyManagement/KeyRingProvider.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,12 @@ private static TimeSpan GetRefreshPeriodWithJitter(TimeSpan refreshPeriod)
259259
// hit a single repository simultaneously. For instance, if the refresh period is 1 hour,
260260
// we'll return a value in the vicinity of 48 - 60 minutes. We use the Random class since
261261
// we don't need a secure PRNG for this.
262-
returnTimeSpan.FromTicks((long)(refreshPeriod.Ticks*(1.0d-(newRandom().NextDouble()/5))));
262+
#ifNET6_0_OR_GREATER
263+
varrandom=Random.Shared;
264+
#else
265+
varrandom=newRandom();
266+
#endif
267+
returnTimeSpan.FromTicks((long)(refreshPeriod.Ticks*(1.0d-(random.NextDouble()/5))));
263268
}
264269

265270
privatestaticDateTimeOffsetMin(DateTimeOffseta,DateTimeOffsetb)

‎src/Http/Routing/test/UnitTests/RouteCollectionTest.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,7 @@ private static RouteCollection GetRouteCollectionWithNamedRoutes(IEnumerable<str
512512

513513
privatestaticRouteCollectionGetNestedRouteCollection(string[]routeNames)
514514
{
515-
varrandom=newRandom();
516-
intindex=random.Next(0,routeNames.Length-1);
515+
intindex=Random.Shared.Next(0,routeNames.Length-1);
517516
varfirst=routeNames.Take(index).ToArray();
518517
varsecond=routeNames.Skip(index).ToArray();
519518

‎src/Identity/Specification.Tests/src/UserManagerSpecificationTests.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,8 @@ public async Task CheckSetEmailValidatesUser()
305305
varmanager=CreateManager();
306306
manager.Options.User.RequireUniqueEmail=true;
307307
manager.UserValidators.Add(newUserValidator<TUser>());
308-
varrandom=newRandom();
309-
varemail="foo"+random.Next()+"@example.com";
310-
varnewEmail="bar"+random.Next()+"@example.com";
308+
varemail="foo"+Random.Shared.Next()+"@example.com";
309+
varnewEmail="bar"+Random.Shared.Next()+"@example.com";
311310
varuser=CreateTestUser(email:email);
312311
IdentityResultAssert.IsSuccess(awaitmanager.CreateAsync(user));
313312
IdentityResultAssert.IsSuccess(awaitmanager.SetEmailAsync(user,newEmail));

‎src/Middleware/HeaderPropagation/samples/HeaderPropagationSample/Startup.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,10 @@ private static StringValues GenerateBetaFeatureOptions()
111111

112112
varthreshold=0.80;// 20% chance for each feature in beta.
113113

114-
varrandom=newRandom();
115114
varvalues=newList<string>();
116115
for(vari=0;i<features.Length;i++)
117116
{
118-
if(random.NextDouble()>threshold)
117+
if(Random.Shared.NextDouble()>threshold)
119118
{
120119
values.Add(features[i]);
121120
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp