Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permute list in place #23

Merged
merged 8 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/delphi/train/permute.py
Original file line number Diff line number Diff line change
@@ -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
SrGonao marked this conversation as resolved.
Show resolved Hide resolved

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if random_number_generator() would return int then you just need to % i (or i+1?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to random uniform and created a generate_number, which generates a number from 0 to max (which was required for the correct shuffling algorithm)

# self.state = self.state + 1
SrGonao marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just return the state as an int
can you add type hint for return btw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why it should return the state, I refactored it in a different way

20 changes: 20 additions & 0 deletions tests/test_permutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from delphi.train.permute import Permutation
SrGonao marked this conversation as resolved.
Show resolved Hide resolved


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
state = perm.state
assert state == 822144001
# 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
SrGonao marked this conversation as resolved.
Show resolved Hide resolved
assert test_list == [5, 6, 4, 10, 7, 2, 1, 3, 8, 9]
Loading