best option to shuffle based on seed

177 views
Skip to first unread message

Victor Tan

unread,
Oct 28, 2024, 11:22:01 AM10/28/24
to cxx
Hi, 

As the STL random number engines and generators are banned, and helpers in base/rand_util.h seems don't support seeded random generators.  What would be the best option to randomly shuffle a container (e.g. std::vector) based on the provided seed?  Basically, we would like to shuffle the list of elements to keep them identical throughout the lifetime of the Chromium major version (it's also the seed), and changes for the new Chromium release major version.  This function will be frequently called during requests, it's not just a one-time call. 

Thanks,
Victor

Peter Kasting

unread,
Oct 28, 2024, 12:46:24 PM10/28/24
to Victor Tan, cxx
Good question. I wonder if there's something domain-specific somewhere in the codebase. Even if we allowed the STL engines and generators, I don't think we guarantee they will be consistent across platforms or between versions, which might risk some unintentional behavior. 

PK

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAJh4P7EMOBRSHN8G_wN%2BibKDrBDB8ASkV%3Dqt_xVMbQnpMN9xZQ%40mail.gmail.com.

Marijn Kruisselbrink

unread,
Oct 28, 2024, 1:10:45 PM10/28/24
to Peter Kasting, Victor Tan, cxx
Unfortunately there is at least one test in the code base already that rely on STL based seeded random number generators to be consistent across platforms and versions. But that's of course no reason to add more code using banned constructs... (I'm not sure how that usage got in in the first place).

Joe Mason

unread,
Oct 28, 2024, 1:12:09 PM10/28/24
to Peter Kasting, Victor Tan, cxx
You should probably do the shuffle at build time, writing the list to a generated file. That way you can add a compile-time test that the same list is generated in every build for the life of the version. (And that it's the same on every platform, if needed.)

> This function will be frequently called during requests, it's not just a one-time call. 

I assume you mean a function that should return the same list, in the same order, is called frequently?

Victor Tan

unread,
Oct 28, 2024, 2:08:33 PM10/28/24
to Joe Mason, Peter Kasting, cxx
> I assume you mean a function that should return the same list, in the same order, is called frequently?

Yes, we expected the function to return the same list element if the Chromium major version is the same. 

Daniel Cheng

unread,
Oct 28, 2024, 2:21:14 PM10/28/24
to Victor Tan, Joe Mason, Peter Kasting, cxx
Does it need to be a consistent ordering across all users of a Chrome major version? Or does it just need to have a stable ordering per user per major revision?

Daniel

Victor Tan

unread,
Oct 28, 2024, 2:38:17 PM10/28/24
to Daniel Cheng, Joe Mason, Peter Kasting, cxx
> Does it need to be a consistent ordering across all users of a Chrome major version? Or does it just need to have a stable ordering per user per major revision?

To keep compat and  minimize caching variance, I think we would like to keep a consistent ordering across all users of a chrome major version.
For the current implementation, we only support at most three elements in the list, we used a stable permutation table: all permutations of {0,1,2} , which are 3!=6, using major version % 6 to get the order. 
We would like to support 3+ elements in the list to achieve the same effect.  
With the STL library, It seems we can't guarantee to get the same sequence when shuffling a list with the same seed across all platforms.

David Benjamin

unread,
Oct 28, 2024, 2:40:58 PM10/28/24
to Victor Tan, Daniel Cheng, Joe Mason, Peter Kasting, cxx
A user-stable, but otherwise randomized, ordering would probably also have privacy implications.

Reply all
Reply to author
Forward
0 new messages