diff --git a/src/delphi/train/permute.py b/src/delphi/train/permute.py new file mode 100644 index 00000000..ced0b886 --- /dev/null +++ b/src/delphi/train/permute.py @@ -0,0 +1,36 @@ +# Number number generation utils without using any library + + +class Permutation: + """ + Permutation class + Atributes: + - state: int, seed for the random number generator + + """ + + def __init__(self, seed): + self.state = seed + + def set_seed(self, seed): + """ + Set the seed + """ + self.state = seed + + def permute(self, list): + """ + Permute a list in place using Fisher-Yates shuffle + """ + for i in range(len(list)): + j = int(self.random_number_generator() * (i + 1)) + # self.state = self.state + 1 + list[i], list[j] = list[j], list[i] + return list + + def random_number_generator(self): + """ + Generate a random number between 0 and 1 using minstd_rand from C++11 + """ + self.state = (self.state * 48271) % 4294967296 + return self.state / 4294967296 diff --git a/tests/test_permutations.py b/tests/test_permutations.py new file mode 100644 index 00000000..8a13fccd --- /dev/null +++ b/tests/test_permutations.py @@ -0,0 +1,18 @@ +from delphi.train.permute import Permutation + + +def test_permute(): + + perm = Permutation(1) + + # Test the random number generator + for i in range(100000): + assert perm.random_number_generator() >= 0 + assert perm.random_number_generator() <= 1 + + # Test the permutation + test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + length = len(test_list) + for i in range(100000): + perm.permute(test_list) + assert len(test_list) == length