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

Fix #38: improve dfbuyer random algorithm #39

Merged

Conversation

trizin
Copy link
Contributor

@trizin trizin commented Aug 15, 2023

Fixes #38

Changes proposed in this PR:

  • improve dfbuyer random algorithm

@trizin trizin linked an issue Aug 15, 2023 that may be closed by this pull request
@trizin trizin requested a review from trentmc August 15, 2023 12:07
@trizin trizin marked this pull request as ready for review August 15, 2023 12:07
@trentmc trentmc changed the title Fix encrypted tx arguments and improve dfbuyer random algorithm Fix #38: improve dfbuyer random algorithm Aug 15, 2023
return [k]
num = random.randint(1, k)
return [num] + numbers_with_sum(n - 1, k - num)
numbers = [random.randint(1, k - n + 1) for _ in range(n - 1)]
Copy link
Member

Choose a reason for hiding this comment

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

You should use random sampling without replacement.

And to do this, use random.sample().

Examples of usage: https://pynative.com/python-random-sample/

Copy link
Member

Choose a reason for hiding this comment

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

Randint is not the way either. You shouldn't have to write a loop. Just use random.sample()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to clarify, in this line I'd like to generate n-1 numbers that are between 1 and k - n + 1. I don't understand how random.sample is going to work in this case. Could you elaborate on that please?

Copy link
Member

Choose a reason for hiding this comment

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

Something like

random.sample(range(1, k - n + 1), n - 1)

It might be that the first argument needs to be 1 larger or smaller. And same for second argument. I defer to you to test.

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 updated it to use random.sample, could you take a look?

pdr_backend/dfbuyer/main.py Outdated Show resolved Hide resolved
pdr_backend/dfbuyer/main.py Outdated Show resolved Hide resolved
pdr_backend/utils/contract.py Outdated Show resolved Hide resolved
@trizin
Copy link
Contributor Author

trizin commented Aug 15, 2023

Thanks @trentmc for the review! I've added docstring, inline comments, tests and type declarations. Also reverted the tx arguments changes.

return [k]
num = random.randint(1, k)
return [num] + numbers_with_sum(n - 1, k - num)
numbers = [random.randint(1, k - n + 1) for _ in range(n - 1)]
Copy link
Member

Choose a reason for hiding this comment

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

Randint is not the way either. You shouldn't have to write a loop. Just use random.sample()

@trizin trizin merged commit 5c2b0f0 into main Aug 16, 2023
4 checks passed
@trizin trizin deleted the issue38-fix-encrypted-tx-arguments-and-dfbuyer-random-algorithm branch August 16, 2023 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix dfbuyer random algorithm
2 participants