-
Notifications
You must be signed in to change notification settings - Fork 711
[css-values-5] seeded random values #12072
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
Comments
Wouldn't it be better to just use Or are you hoping for a mix of |
In function pseudo_shuffled_set() {
// A set of options
$options = array(
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
);
// use the request url as the random seed
$request_seed = wp_unslash( $_SERVER['REQUEST_URI'] ?? '' );
srand( crc32( $request_seed ) );
// shuffle the array
$copy = array( ...$options );
$copy_length = count( $copy ) > 0;
$shuffled = array();
while ( $copy_length > 0 ) {
// `rand` is seeded and will yield deterministic results
$i = rand( 0, count( $copy ) - 1 );
$shuffled[] = array_splice( $copy, $i, 1 )[0];
$copy_length = count( $copy ) > 0;
}
return $shuffled;
} Then when rendering a templates page I can create a shuffled set that is specific to that page and use it to add some visual variation to elements. Each page will be different from every other page (to some degree), but rendering the same page twice will yield the same result. The current
I not familiar with GLSL, but this does sound correct :) |
Making the random values change on page load is definitely an intentional design decision for now, but allowing that aspect of the caching key to be overridden (currently there's no way to interacte with it) is definitely a possibility. Something like adding a |
Being able to “freeze” the seed between page loads would be invaluable for visual regression testing tools, as otherwise it will be tricky to make them stable when the random is involved. |
Yup, definitely a fair argument. |
To make pages more dynamic and whimsical I sometimes use seeded random values.
These are generated server-side and I will typically use the request url to compute the initial random seed. Each subsequent call to the random function returns a new value.
I find that this gives good dynamic results that are still deterministic.
Visiting the same page twice doesn't result in different outcomes.
But each individual page has their own distinct and seemingly random patterns.
This isn't currently possible with the
random()
function.Could this be done with something like counters? Except that
counter()
returns a string, and not a number. So it can't currently be used afixed <number>
.The text was updated successfully, but these errors were encountered: