Skip to content

Commit d386af2

Browse files
committed
modulo iteration util
1 parent 2fc389a commit d386af2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

math/MathUtil.cs

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34

45
namespace g3
@@ -643,5 +644,20 @@ public static int PowerOf10(int n) {
643644
}
644645

645646

647+
/// <summary>
648+
/// Iterate from 0 to (nMax-1) using prime-modulo, so we see every index once, but not in-order
649+
/// </summary>
650+
public static IEnumerable<int> ModuloIteration(int nMaxExclusive, int nPrime = 31337)
651+
{
652+
int i = 0;
653+
bool done = false;
654+
while (done == false) {
655+
yield return i;
656+
i = (i + nPrime) % nMaxExclusive;
657+
done = (i == 0);
658+
}
659+
}
660+
661+
646662
}
647663
}

0 commit comments

Comments
 (0)