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

Seeded Random Shuffle #1110

Merged
merged 4 commits into from
Dec 3, 2019
Merged

Seeded Random Shuffle #1110

merged 4 commits into from
Dec 3, 2019

Conversation

KtorZ
Copy link
Member

@KtorZ KtorZ commented Dec 3, 2019

Issue Number

#1102

Overview

  • 995e006 move 'shuffle' (and specs) into a separate module
    In order to extend this and use it in other places than just the coin selection
    it makes sense to give it a proper home.

  • 9064a78 replace 'Crypto.Number.Generate' by 'System.Random'
    We don't need a crypto-resistent generator here and a system generator
    will do for shuffling outputs or stake pools

  • 5a7bb37 allow shuffle to be seeded with a 'RandomGen'

  • 7c95cec implement 'mkSeed' to generate a seed from a text
    This would be pretty useful to bridge use any entity (e.g. WalletId) with a
    ToText instance as a seed for such generator

Comments

Data.Vector.Shuffle
  shuffle
    every non-empty list can be shuffled, ultimately
      +++ OK, passed 100 tests (92% shuffled).
    shuffle is non-deterministic
      +++ OK, passed 100 tests (92% not deterministic).
    sort (shuffled xs) == sort xs
      +++ OK, passed 100 tests (93% non-empty).
  shuffleWith / mkSeed
    shuffling with the same seed is deterministic
      +++ OK, passed 100 tests (94% non singleton).
    different seed means different shuffles
      +++ OK, passed 200 tests; 21 discarded:
      91.0% different
      
      66.0% big list
      28.0% small list
       6.0% singleton

In order to extend this and use it in other places than just the coin selection
it makes sense to give it a proper home.
We don't need a crypto-resistent generator here and a system generator
will do for shuffling outputs or stake pools
This would be pretty useful to bridge use any entity (e.g. WalletId) with a
ToText instance as a seed for such generator
@KtorZ KtorZ requested a review from Anviking December 3, 2019 14:41
@KtorZ KtorZ self-assigned this Dec 3, 2019
@KtorZ KtorZ mentioned this pull request Dec 3, 2019
2 tasks
Copy link
Member

@Anviking Anviking left a comment

Choose a reason for hiding this comment

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

Neat!

@@ -24,7 +24,7 @@ shuffle :: [a] -> IO [a]
shuffle = modifyInPlace $ \v -> do
let (lo, hi) = (0, MV.length v - 1)
forM_ [lo .. hi] $ \i -> do
j <- fromInteger <$> generateBetween (fromIntegral lo) (fromIntegral hi)
j <- fromInteger <$> randomRIO (fromIntegral lo, fromIntegral hi)
Copy link
Member

Choose a reason for hiding this comment

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

Both take closed intervals, so should be the same 👍

-- ∃(Δ: Int).
-- ∀(es :: [a]).
--
-- g0 ≠g1, length es > Δ⇒ shuffleWith g0 es ≠shuffleWith g1 es
Copy link
Member

Choose a reason for hiding this comment

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

Nit-pick / confusion: the ∃(Δ: Int) part sounds 1) questionable to me, and 2) not the same as the code?. Whatever , given enough time, this property is always disprovable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, to be honest, I was hoping to have the property to fail and have to find a threshold manually (even though this is entirely subjective to the QC generator).

Still, it's hard to translate in pure maths here because that's really a probabilistic results. We can't possibly guarantee that this is true for any list and any seed. But, most of the time, it should be a reasonable assumption. In particular, when lists are big enough (and it's this is what the threshold translates), it should hold with a reasonable confidence.

Turns out that, 90% of the results satisfies the property already with a threshold of 0 🤷‍♂️, QC isn't generating that many singletons.

@KtorZ
Copy link
Member Author

KtorZ commented Dec 3, 2019

bors r+

iohk-bors bot added a commit that referenced this pull request Dec 3, 2019
1110: Seeded Random Shuffle r=KtorZ a=KtorZ

# Issue Number

<!-- Put here a reference to the issue this PR relates to and which requirements it tackles -->

#1102 

# Overview

<!-- Detail in a few bullet points the work accomplished in this PR -->

- 995e006 move 'shuffle' (and specs) into a separate module
    In order to extend this and use it in other places than just the coin selection
    it makes sense to give it a proper home.

- 9064a78 replace 'Crypto.Number.Generate' by 'System.Random'
    We don't need a crypto-resistent generator here and a system generator
    will do for shuffling outputs or stake pools

- 5a7bb37 allow shuffle to be seeded with a 'RandomGen'

- 7c95cec implement 'mkSeed' to generate a seed from a text
    This would be pretty useful to bridge use any entity (e.g. WalletId) with a
    ToText instance as a seed for such generator

# Comments

<!-- Additional comments or screenshots to attach if any -->

```
Data.Vector.Shuffle
  shuffle
    every non-empty list can be shuffled, ultimately
      +++ OK, passed 100 tests (92% shuffled).
    shuffle is non-deterministic
      +++ OK, passed 100 tests (92% not deterministic).
    sort (shuffled xs) == sort xs
      +++ OK, passed 100 tests (93% non-empty).
  shuffleWith / mkSeed
    shuffling with the same seed is deterministic
      +++ OK, passed 100 tests (94% non singleton).
    different seed means different shuffles
      +++ OK, passed 200 tests; 21 discarded:
      91.0% different
      
      66.0% big list
      28.0% small list
       6.0% singleton
```


<!-- 
Don't forget to:

 ✓ Self-review your changes to make sure nothing unexpected slipped through
 ✓ Assign yourself to the PR
 ✓ Assign one or several reviewer(s)
 ✓ Once created, link this PR to its corresponding ticket
 ✓ Assign the PR to a corresponding milestone
 ✓ Acknowledge any changes required to the Wiki
-->


Co-authored-by: KtorZ <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Dec 3, 2019

Build succeeded

@iohk-bors iohk-bors bot merged commit 7c95cec into master Dec 3, 2019
@KtorZ KtorZ deleted the KtorZ/WB-34/seeded-random-shuffle branch December 11, 2019 09:26
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.

2 participants