Skip to content

Commit 8ff9eea

Browse files
authored
Random.Shared 2 (#239)
* net8 - some more new Random() => Random.Shared * net7 * net6
1 parent af708d8 commit 8ff9eea

File tree

39 files changed

+75
-129
lines changed

39 files changed

+75
-129
lines changed

6.0/BlazorSample_Server/Pages/call-js-from-dotnet/CallJsExample2.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
}
1414

1515
@code {
16-
private Random r = new();
1716
private string? stockSymbol;
1817
private decimal price;
1918

2019
private async Task SetStock()
2120
{
2221
stockSymbol =
23-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
24-
price = r.Next(1, 101);
22+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
23+
price = Random.Shared.Next(1, 101);
2524
await JS.InvokeVoidAsync("displayTickerAlert1", stockSymbol, price);
2625
}
2726
}

6.0/BlazorSample_Server/Pages/call-js-from-dotnet/CallJsExample3.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
}
1515

1616
@code {
17-
private Random r = new();
1817
private string? stockSymbol;
1918
private decimal price;
2019
private JsInteropClasses1? jsClass;
@@ -29,8 +28,8 @@
2928
if (jsClass is not null)
3029
{
3130
stockSymbol =
32-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
33-
price = r.Next(1, 101);
31+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
32+
price = Random.Shared.Next(1, 101);
3433
await jsClass.TickerChanged(stockSymbol, price);
3534
}
3635
}

6.0/BlazorSample_Server/Pages/call-js-from-dotnet/CallJsExample4.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
}
1919

2020
@code {
21-
private Random r = new();
2221
private string? stockSymbol;
2322
private decimal price;
2423
private string? result;
2524

2625
private async Task SetStock()
2726
{
2827
stockSymbol =
29-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
30-
price = r.Next(1, 101);
28+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
29+
price = Random.Shared.Next(1, 101);
3130
var interopResult =
3231
await JS.InvokeAsync<string>("displayTickerAlert2", stockSymbol, price);
3332
result = $"Result of TickerChanged call for {stockSymbol} at " +

6.0/BlazorSample_Server/Pages/call-js-from-dotnet/CallJsExample5.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
}
2020

2121
@code {
22-
private Random r = new();
2322
private string? stockSymbol;
2423
private decimal price;
2524
private JsInteropClasses2? jsClass;
@@ -35,8 +34,8 @@
3534
if (jsClass is not null)
3635
{
3736
stockSymbol =
38-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
39-
price = r.Next(1, 101);
37+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
38+
price = Random.Shared.Next(1, 101);
4039
var interopResult = await jsClass.TickerChanged(stockSymbol, price);
4140
result = $"Result of TickerChanged call for {stockSymbol} at " +
4241
$"{price.ToString("c")}: {interopResult}";

6.0/BlazorSample_Server/Pages/data-binding/Parent1.razor

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
<ChildBind @bind-Year="year" />
1010

1111
@code {
12-
private Random r = new();
1312
private int year = 1979;
1413

1514
private void UpdateYear()
1615
{
17-
year = r.Next(1950, 2021);
16+
year = Random.Shared.Next(1950, 2021);
1817
}
1918
}

6.0/BlazorSample_Server/Shared/data-binding/ChildBind.razor

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
</div>
1010

1111
@code {
12-
private Random r = new();
13-
1412
[Parameter]
1513
public int Year { get; set; }
1614

@@ -19,6 +17,6 @@
1917

2018
private async Task UpdateYearFromChild()
2119
{
22-
await YearChanged.InvokeAsync(r.Next(1950, 2021));
20+
await YearChanged.InvokeAsync(Random.Shared.Next(1950, 2021));
2321
}
2422
}

6.0/BlazorSample_WebAssembly/Pages/call-js-from-dotnet/CallJsExample2.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
}
1414

1515
@code {
16-
private Random r = new();
1716
private string? stockSymbol;
1817
private decimal price;
1918

2019
private async Task SetStock()
2120
{
2221
stockSymbol =
23-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
24-
price = r.Next(1, 101);
22+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
23+
price = Random.Shared.Next(1, 101);
2524
await JS.InvokeVoidAsync("displayTickerAlert1", stockSymbol, price);
2625
}
2726
}

6.0/BlazorSample_WebAssembly/Pages/call-js-from-dotnet/CallJsExample3.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
}
1515

1616
@code {
17-
private Random r = new();
1817
private string? stockSymbol;
1918
private decimal price;
2019
private JsInteropClasses1? jsClass;
@@ -29,8 +28,8 @@
2928
if (jsClass is not null)
3029
{
3130
stockSymbol =
32-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
33-
price = r.Next(1, 101);
31+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
32+
price = Random.Shared.Next(1, 101);
3433
await jsClass.TickerChanged(stockSymbol, price);
3534
}
3635
}

6.0/BlazorSample_WebAssembly/Pages/call-js-from-dotnet/CallJsExample4.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
}
1919

2020
@code {
21-
private Random r = new();
2221
private string? stockSymbol;
2322
private decimal price;
2423
private string? result;
2524

2625
private async Task SetStock()
2726
{
2827
stockSymbol =
29-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
30-
price = r.Next(1, 101);
28+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
29+
price = Random.Shared.Next(1, 101);
3130
var interopResult =
3231
await JS.InvokeAsync<string>("displayTickerAlert2", stockSymbol, price);
3332
result = $"Result of TickerChanged call for {stockSymbol} at " +

6.0/BlazorSample_WebAssembly/Pages/call-js-from-dotnet/CallJsExample5.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
}
2020

2121
@code {
22-
private Random r = new();
2322
private string? stockSymbol;
2423
private decimal price;
2524
private JsInteropClasses2? jsClass;
@@ -35,8 +34,8 @@
3534
if (jsClass is not null)
3635
{
3736
stockSymbol =
38-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
39-
price = r.Next(1, 101);
37+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
38+
price = Random.Shared.Next(1, 101);
4039
var interopResult = await jsClass.TickerChanged(stockSymbol, price);
4140
result = $"Result of TickerChanged call for {stockSymbol} at " +
4241
$"{price.ToString("c")}: {interopResult}";

6.0/BlazorSample_WebAssembly/Pages/data-binding/Parent1.razor

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
<ChildBind @bind-Year="year" />
1010

1111
@code {
12-
private Random r = new();
1312
private int year = 1979;
1413

1514
private void UpdateYear()
1615
{
17-
year = r.Next(1950, 2021);
16+
year = Random.Shared.Next(1950, 2021);
1817
}
1918
}

6.0/BlazorSample_WebAssembly/Shared/data-binding/ChildBind.razor

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
</div>
1010

1111
@code {
12-
private Random r = new();
13-
1412
[Parameter]
1513
public int Year { get; set; }
1614

@@ -19,6 +17,6 @@
1917

2018
private async Task UpdateYearFromChild()
2119
{
22-
await YearChanged.InvokeAsync(r.Next(1950, 2021));
20+
await YearChanged.InvokeAsync(Random.Shared.Next(1950, 2021));
2321
}
2422
}

6.0/BlazorServerEFCoreSample/Data/SeedContacts.cs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44

@@ -168,19 +168,14 @@ public class SeedContacts
168168
"VA", "WA", "WV", "WI", "WY"
169169
};
170170

171-
/// <summary>
172-
/// Get some randominzation in play.
173-
/// </summary>
174-
private readonly Random _random = new();
175-
176171
/// <summary>
177172
/// Picks a random item from a list.
178173
/// </summary>
179174
/// <param name="list">A list of <c>string</c> to parse.</param>
180175
/// <returns>A single item from the list.</returns>
181176
private string RandomOne(string[] list)
182177
{
183-
var idx = _random.Next(list.Length - 1);
178+
var idx = Random.Shared.Next(list.Length - 1);
184179
return list[idx];
185180
}
186181

@@ -194,12 +189,12 @@ private Contact MakeContact()
194189
{
195190
FirstName = RandomOne(_gems),
196191
LastName = $"{RandomOne(_colors)}{RandomOne(_things)}",
197-
Phone = $"({_random.Next(100, 999)})-555-{_random.Next(1000, 9999)}",
198-
Street = $"{_random.Next(1, 99999)} {_random.Next(1, 999)}" +
192+
Phone = $"({Random.Shared.Next(100, 999)})-555-{Random.Shared.Next(1000, 9999)}",
193+
Street = $"{Random.Shared.Next(1, 99999)} {Random.Shared.Next(1, 999)}" +
199194
$" {RandomOne(_streets)} {RandomOne(_streetTypes)} {RandomOne(_directions)}",
200195
City = RandomOne(_cities),
201196
State = RandomOne(_states),
202-
ZipCode = $"{ _random.Next(10000, 99999)}"
197+
ZipCode = $"{Random.Shared.Next(10000, 99999)}"
203198
};
204199
return contact;
205200
}

7.0/BlazorSample_Server/Pages/call-js-from-dotnet/CallJsExample2.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
}
1414

1515
@code {
16-
private Random r = new();
1716
private string? stockSymbol;
1817
private decimal price;
1918

2019
private async Task SetStock()
2120
{
2221
stockSymbol =
23-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
24-
price = r.Next(1, 101);
22+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
23+
price = Random.Shared.Next(1, 101);
2524
await JS.InvokeVoidAsync("displayTickerAlert1", stockSymbol, price);
2625
}
2726
}

7.0/BlazorSample_Server/Pages/call-js-from-dotnet/CallJsExample3.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
}
1515

1616
@code {
17-
private Random r = new();
1817
private string? stockSymbol;
1918
private decimal price;
2019
private JsInteropClasses1? jsClass;
@@ -29,8 +28,8 @@
2928
if (jsClass is not null)
3029
{
3130
stockSymbol =
32-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
33-
price = r.Next(1, 101);
31+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
32+
price = Random.Shared.Next(1, 101);
3433
await jsClass.TickerChanged(stockSymbol, price);
3534
}
3635
}

7.0/BlazorSample_Server/Pages/call-js-from-dotnet/CallJsExample4.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
}
1919

2020
@code {
21-
private Random r = new();
2221
private string? stockSymbol;
2322
private decimal price;
2423
private string? result;
2524

2625
private async Task SetStock()
2726
{
2827
stockSymbol =
29-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
30-
price = r.Next(1, 101);
28+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
29+
price = Random.Shared.Next(1, 101);
3130
var interopResult =
3231
await JS.InvokeAsync<string>("displayTickerAlert2", stockSymbol, price);
3332
result = $"Result of TickerChanged call for {stockSymbol} at " +

7.0/BlazorSample_Server/Pages/call-js-from-dotnet/CallJsExample5.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
}
2020

2121
@code {
22-
private Random r = new();
2322
private string? stockSymbol;
2423
private decimal price;
2524
private JsInteropClasses2? jsClass;
@@ -35,8 +34,8 @@
3534
if (jsClass is not null)
3635
{
3736
stockSymbol =
38-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
39-
price = r.Next(1, 101);
37+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
38+
price = Random.Shared.Next(1, 101);
4039
var interopResult = await jsClass.TickerChanged(stockSymbol, price);
4140
result = $"Result of TickerChanged call for {stockSymbol} at " +
4241
$"{price.ToString("c")}: {interopResult}";

7.0/BlazorSample_Server/Pages/data-binding/Parent1.razor

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
<ChildBind @bind-Year="year" />
1010

1111
@code {
12-
private Random r = new();
1312
private int year = 1979;
1413

1514
private void UpdateYear()
1615
{
17-
year = r.Next(1950, 2021);
16+
year = Random.Shared.Next(1950, 2021);
1817
}
1918
}

7.0/BlazorSample_Server/Shared/data-binding/ChildBind.razor

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
</div>
1010

1111
@code {
12-
private Random r = new();
13-
1412
[Parameter]
1513
public int Year { get; set; }
1614

@@ -19,6 +17,6 @@
1917

2018
private async Task UpdateYearFromChild()
2119
{
22-
await YearChanged.InvokeAsync(r.Next(1950, 2021));
20+
await YearChanged.InvokeAsync(Random.Shared.Next(1950, 2021));
2321
}
2422
}

7.0/BlazorSample_WebAssembly/Pages/call-js-from-dotnet/CallJsExample2.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
}
1414

1515
@code {
16-
private Random r = new();
1716
private string? stockSymbol;
1817
private decimal price;
1918

2019
private async Task SetStock()
2120
{
2221
stockSymbol =
23-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
24-
price = r.Next(1, 101);
22+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
23+
price = Random.Shared.Next(1, 101);
2524
await JS.InvokeVoidAsync("displayTickerAlert1", stockSymbol, price);
2625
}
2726
}

7.0/BlazorSample_WebAssembly/Pages/call-js-from-dotnet/CallJsExample3.razor

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
}
1515

1616
@code {
17-
private Random r = new();
1817
private string? stockSymbol;
1918
private decimal price;
2019
private JsInteropClasses1? jsClass;
@@ -29,8 +28,8 @@
2928
if (jsClass is not null)
3029
{
3130
stockSymbol =
32-
$"{(char)('A' + r.Next(0, 26))}{(char)('A' + r.Next(0, 26))}";
33-
price = r.Next(1, 101);
31+
$"{(char)('A' + Random.Shared.Next(0, 26))}{(char)('A' + Random.Shared.Next(0, 26))}";
32+
price = Random.Shared.Next(1, 101);
3433
await jsClass.TickerChanged(stockSymbol, price);
3534
}
3635
}

0 commit comments

Comments
 (0)