Skip to content

Commit ea68368

Browse files
Update api template to use new Random APIs (#46298)
Fixes #46087
1 parent cc40c1d commit ea68368

File tree

1 file changed

+11
-16
lines changed
  • src/ProjectTemplates/Web.ProjectTemplates/content/Api-CSharp

1 file changed

+11
-16
lines changed

src/ProjectTemplates/Web.ProjectTemplates/content/Api-CSharp/Todo.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,38 @@ private static readonly (string[] Prefixes, string[] Suffixes)[] _parts = new[]
2222

2323
internal static IEnumerable<Todo> GenerateTodos(int count = 5)
2424
{
25-
var titleMap = new List<(int Row, int Prefix, int Suffix)>();
25+
var titleCount = _parts.Sum(row => row.Prefixes.Length * row.Suffixes.Length);
26+
var titleMap = new (int Row, int Prefix, int Suffix)[titleCount];
27+
var mapCount = 0;
2628
for (var i = 0; i < _parts.Length; i++)
2729
{
2830
var prefixes = _parts[i].Prefixes;
2931
var suffixes = _parts[i].Suffixes;
30-
for (int j = 0; j < prefixes.Length; j++)
32+
for (var j = 0; j < prefixes.Length; j++)
3133
{
32-
for (int k = 0; k < suffixes.Length; k++)
34+
for (var k = 0; k < suffixes.Length; k++)
3335
{
34-
titleMap.Add((i, j, k));
36+
titleMap[mapCount++] = (i, j, k);
3537
}
3638
}
3739
}
3840

39-
var random = new Random();
41+
Random.Shared.Shuffle(titleMap);
4042

4143
for (var id = 1; id <= count; id++)
4244
{
45+
var (rowIndex, prefixIndex, suffixIndex) = titleMap[id];
46+
var (prefixes, suffixes) = _parts[rowIndex];
4347
yield return new Todo
4448
{
4549
Id = id,
46-
Title = GetNextTitle(),
47-
DueBy = random.Next(-200, 365) switch
50+
Title = string.Join(' ', prefixes[prefixIndex], suffixes[suffixIndex]),
51+
DueBy = Random.Shared.Next(-200, 365) switch
4852
{
4953
< 0 => null,
5054
var days => DateOnly.FromDateTime(DateTime.Now.AddDays(days))
5155
}
5256
};
53-
54-
string GetNextTitle()
55-
{
56-
var index = random.Next(0, titleMap.Count - 1);
57-
var map = titleMap[index];
58-
var row = _parts[map.Row];
59-
titleMap.RemoveAt(index);
60-
return string.Join(' ', row.Prefixes[map.Prefix], row.Suffixes[map.Suffix]);
61-
}
6257
}
6358
}
6459
}

0 commit comments

Comments
 (0)