Skip to content
This repository was archived by the owner on Mar 8, 2025. It is now read-only.

Commit a420255

Browse files
committed
Initial IEnumerable
1 parent 60485f5 commit a420255

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

AMT.LinqExtensions/LinqExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public static T Random<T> (this ICollection<T> elements)
2424
return elements.ElementAt(_randomizer.Next(0, elements.Count - 1));
2525
}
2626

27+
public static T Random<T> (this IEnumerable<T> elements)
28+
{
29+
return elements.ElementAt(_randomizer.Next(0, elements.Count() - 1));
30+
}
31+
2732
#endregion Public LINQ methods
2833
}
2934

netcore-extensions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 1ad4abd22374234c6e60c1a7e548a75ea84e8df9

restify-sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit b8bda74535cc756f57160939c19b79947da677ed

test/LinqExtensionsTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ public void random_element_from_int_list()
3333
}
3434

3535

36+
[Fact]
37+
public void random_element_from_int_list_by_IEnumerable()
38+
{
39+
List<int> list = new List<int>();
40+
for (int i = 0; i < 250; ++i) { list.Add(i); }
41+
42+
IEnumerable<int> enumr = list;
43+
foreach (var itr in enumr)
44+
{
45+
int curr = enumr.Random();
46+
curr.Should().BeGreaterThan(-1);
47+
}
48+
}
49+
50+
3651
[Fact]
3752
public void random_element_from_single_item_list()
3853
{
@@ -48,6 +63,25 @@ public void random_element_from_single_item_list()
4863
}
4964
}
5065

66+
67+
[Fact]
68+
public void random_element_from_()
69+
{
70+
71+
SortedList<int, string> list = new SortedList<int, string>();
72+
for (int i = 0; i < 250; ++i) { list.Add(i, i.ToString()); }
73+
74+
// Get random elements from ~1/10 of the list. The probability of sequential duplicates
75+
// increases when covering more of the available elements.
76+
KeyValuePair<int, string> prev = new KeyValuePair<int, string>();
77+
for (int i = 0; i < list.Count/10; ++i)
78+
{
79+
var curr = list.Random();
80+
curr.Should().NotBe(prev);
81+
prev = curr;
82+
}
83+
}
84+
5185
#endregion Positive tests
5286

5387

0 commit comments

Comments
 (0)