Skip to content

Implementations of SFC32/SFC64 chaotic RNGs #65

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

tertu-m
Copy link

@tertu-m tertu-m commented Jun 20, 2025

This adds an implementation of the SFC32 and SFC64 chaotic RNGs.

There's some work left to be done regarding docs and formatting but they should work; I would like feedback on that.

EDIT: I'm also not even sure if this would be useful. One advantage it does have, perhaps, is that it does not require multiplication at all.

Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

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

At a glance, this seems like something we could add. But should we?

  • I'd like some justification of why this is useful to people.
  • Would you be willing to maintain this in case of future bug reports / requests?

@@ -0,0 +1,39 @@
// Copyright 2025 XXX.
Copy link
Member

Choose a reason for hiding this comment

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

Unfinished?

Copy link
Author

@tertu-m tertu-m Jun 21, 2025

Choose a reason for hiding this comment

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

I'll update that to mention the project now.

@tertu-m
Copy link
Author

tertu-m commented Jun 21, 2025

I would be willing to maintain this in the future.

My reasons for suggesting they are included that they are simple, fast generators (in my experience and based on what I could find on the internet they are slightly faster than Xoshiro and faster than PCG), they are high quality, they are reasonably well known as far as I am aware, and they are structurally different from the existing generators present in rand.

Additionally, in principle, stream support could be added as I mentioned in the comments, and this has been done by some people, but I don't think that functionality is very well studied. It is known that correlations exist early in two streams with different increments if the initial values provided for a, b, and c are the same.

EDIT: I guess you knew all that already; I read the comments later where that was mentioned. Anyway I guess it had been talked about in the past and the thought was maybe one more choice would be nice? This implementation doesn't have the counter panic issue, anyway.

Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

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

I'll accept that argument.

I started a review, but didn't get as far as the actual RNG code yet.

Comment on lines 9 to 10
[features]
serde1 = ["serde"]
Copy link
Member

Choose a reason for hiding this comment

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

Please use serde = ["dep:serde"]

Comment on lines 65 to 67
// PracRand uses different mixing step counts for different types of seeds.
// Here, just use one of the larger values always.
const SEED_MIXING_STEPS: u32 = 16;
Copy link
Member

Choose a reason for hiding this comment

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

PracRand uses 8 for seed_fast(u64), 12 for seed(u64) or 15 for seed(u32, u32, u32). Perhaps we should use the latter?

Copy link
Author

Choose a reason for hiding this comment

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

I will do that.

Comment on lines 97 to 103
fn reference() {
// These values were produced with the reference implementation:
// https://pracrand.sourceforge.net/
let mut rng = Sfc32::from_seed([0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0]);
let expected: [u32; 16] = [
0xA87DBC7E,
0x1787178C,
Copy link
Member

Choose a reason for hiding this comment

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

Instructions to reproduce would be nice. My hack is something like:

  1. Tweak the number of mixing steps to match (sfc.cpp:92)
  2. Compile: for s in $(find src -name *.cpp); do g++ -I include -c $s; done
  3. g++ -I include -c tools/RNG_output.cpp
  4. g++ *.o -o RNG_output
  5. ./RNG_output sfc32 8 0x100000002 | hexdump
    hexdump is clearly not the right tool here; we want to read 32-bit LE values not 16-bit LE.

Copy link
Author

Choose a reason for hiding this comment

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

I'll put together a procedure, in all honesty I just spit the values into a file that I then opened in a hex editor and copied in...

Copy link
Author

@tertu-m tertu-m Jun 25, 2025

Choose a reason for hiding this comment

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

Okay so the procedure I came up with is basically:

  1. Compile PractRand as specified in https://pracrand.sourceforge.net/installation.txt.
  2. For Sfc32 run ./RNG_output sfc32 76 0000000100000002 | od -j 12 -t x4 -v (skipping 12 bytes to account for the extra 3 mixing steps). For Sfc64 it's ./RNG_output sfc64 176 0000000000000001 | od -j 48 -t x8 -v.

Copy link
Member

Choose a reason for hiding this comment

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

Your link is wrong (it points at github).

Ah, I didn't know od.

tertu-m added 4 commits June 25, 2025 09:01
* add PractRand-style seed_from_u64 implementations
* Cargo.toml changes
* TODO: seed_from_u64 tests, readme?
The readme links are partially dead for now, as the repo isn't part of upstream
Comment on lines 9 to 15
// A decent-quality 64 bit linear congruential generator used to extend seeds.
// Parameters from M. E. O'Neill.
pub fn seed_extender_lcg (state: u64) -> u64 {
let multiplier: u64 = 9199940308585234877;

state.wrapping_mul(multiplier).wrapping_add(multiplier)
}
Copy link
Member

Choose a reason for hiding this comment

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

Your code here is essentially a 64-bit LCG with no output permutation or truncation; it appears to have been copied from https://www.pcg-random.org/posts/does-it-beat-the-minimal-standard.html but misses the output function (right shift).

If I recall correctly, the output size being smaller than the LCG state is a key part of ensuring good statistical performance of LCGs — though this may be less important here where only a few output values are used; I don't know enough to say for sure.

However, I still don't understand why you don't want to use the default seed_from_u64 implementation which uses PCG32 — essentially a 64-bit LCG with 32-bit permuted output?

Copy link
Author

@tertu-m tertu-m Jul 1, 2025

Choose a reason for hiding this comment

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

I think I probably took the "we are committing to stability of its output" comment a little more seriously than I should have. If you think that using the default implementation is OK, then we can do that, and I agree that would be better.

As for the LCG:
Yes, that's where I got that LCG from, I would have put the link but I figured that mentioning the author's name was enough (as it's a bog-standard LCG, just with uncommon parameters), but I can add the link if you need. My motivation for just using an LCG was principally it needing fewer lines of code than a PCG.

The reason I skipped the output function (and only for Sfc64, Sfc32 effectively does use it) was that based on the testing that I did the quality of the generator was basically irrelevant, it just had to be something. So I figured using all the bits from an LCG would be good enough, as it would get run through the Sfc64 mixer anyway.

Copy link
Member

Choose a reason for hiding this comment

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

I think I probably took the "we are committing to stability of its output" comment a little more seriously than I should have.

Sorry for not being clearer. No, I just wanted to make a note of this.

I would have put the link but ...

I just searched for the constant. Easy.

Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

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

LGTM except:

  • run cargo fmt
  • Move the serde imports from lib.rs to sfc*.rs and test with serde
  • Rebase or merge master once #66 is merged

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