News for hackers. Stuff that matters.
Re:Rolling the Dice with CSS Random() by gherkinnn on Sunday August 24, 2025 @01:11AM (#45001563)
Nice. Currently I have to set CSS custom properties with JS to achieve the same effect.

Wonderful to see how CSS gets a usable random function before JS does.

Re:Rolling the Dice with CSS Random() by noman-land on Sunday August 24, 2025 @01:15AM (#45001587)
Maybe "usable" is your qualifier but what's wrong with Math.random()?
Re:Rolling the Dice with CSS Random() by tsujp on Sunday August 24, 2025 @03:39AM (#45002179)
JS also has Crypto.getRandomValues()
Re:Rolling the Dice with CSS Random() by akdev1l on Sunday August 24, 2025 @05:04AM (#45002629)
To generate random number in a specific range you need to do something I always forget and need to google.

    Math.floor(Math.random() * (max - min + 1)) + min;

(Google AI summary says this is the thing)

The CSS function would be random(min, max)

Also the CSS function seems to take a number of steps, it is not immediately obvious to me how to do that with Math.random()

Re:Rolling the Dice with CSS Random() by sdenton4 on Sunday August 24, 2025 @07:55AM (#45003544)
Why not add a Math.randint?

I imagine there's some deep ideological war over whether to add more programming functionality to css...

Re:Rolling the Dice with CSS Random() by ameliaquining on Sunday August 24, 2025 @08:11PM (#45009004)
Currently under discussion in the standards committee: https://tc39.es/proposal-random-functions/
Re:Rolling the Dice with CSS Random() by ameliaquining on Sunday August 24, 2025 @08:19PM (#45009034)

    Math.floor(Math.random() * Math.floor((max - min) / step)) * step + min
Re:Rolling the Dice with CSS Random() by ballenf on Sunday August 24, 2025 @08:15AM (#45003664)
So now we can add a random data prop to a hidden dom element, then query that from JS. You know, to make your JS random function simpler. ;)
Re:Rolling the Dice with CSS Random() by gherkinnn on Sunday August 24, 2025 @02:56PM (#45006694)
That was my second idea. I've done worse.
Re:Rolling the Dice with CSS Random() by jvdvegt on Sunday August 24, 2025 @02:31AM (#45001875)
Nice but... no dice!
Re:Rolling the Dice with CSS Random() by Tepix on Sunday August 24, 2025 @03:58AM (#45002284)
Related: Animated starfield in pure CSS

https://codepen.io/ArneSava/pen/BaWxOaR

Re:Rolling the Dice with CSS Random() by lelandfe on Sunday August 24, 2025 @08:52AM (#45003893)
Really laggy on an M1 MBP; probably `box-shadow`'s fault.
Re:Rolling the Dice with CSS Random() by Tepix on Sunday August 24, 2025 @09:29AM (#45004123)
Have you tried different browsers?
Re:Rolling the Dice with CSS Random() by kachapopopow on Sunday August 24, 2025 @04:47AM (#45002556)
I yern for the day we can have react-type pages without any javascript. Keep chugging webkit I believe in you.
Re:Rolling the Dice with CSS Random() by hannob on Sunday August 24, 2025 @06:04AM (#45002921)
Having seen too many "this randomness function was never meant to be used for security, but people use it for security anyway" vulnerabilities in the past:

Can we PLEASEPLEASEPLEASE have this secure by default from the beginning?

Re:Rolling the Dice with CSS Random() by 1718627440 on Sunday August 24, 2025 @06:44AM (#45003105)
Security in the Stylesheet? Come on, you need to set boundaries for expectable use.
Re:Rolling the Dice with CSS Random() by phyzome on Sunday August 24, 2025 @08:16AM (#45003679)
"Look, I implemented diceware in pure CSS!" is unfortunately not that hard to imagine.

I would bet someone is already working on it as we speak.

Re:Rolling the Dice with CSS Random() by 1718627440 on Sunday August 24, 2025 @01:24PM (#45005965)
I don't disagree on that point.

Introducing cryptography in the STANDARD for stylesheets adds complexity where it doesn't belong. Ultimately a browser vendor isn't responsible when a company sells insecure cryptography.

Adding crypto to CSS will bring us nearer to bitcoin mining in the CSS engine.

Re:Rolling the Dice with CSS Random() by bigDinosaur on Sunday August 24, 2025 @08:20AM (#45003702)
If you implement security protocols in a production app using CSS then you deserve to be hacked and then sued for negligence.
Re:Rolling the Dice with CSS Random() by RestartKernel on Sunday August 24, 2025 @01:00PM (#45005797)
Counterargument: it would make for a very funny post-mortem.
Re:Rolling the Dice with CSS Random() by demurgos on Sunday August 24, 2025 @07:11AM (#45003267)
Where is the spec? I can't find an entry on MDN.

Is there a way to get reproducibility? In the same browser or across browsers? Even if it's not the default mode.

Re:Rolling the Dice with CSS Random() by twiss on Sunday August 24, 2025 @07:20AM (#45003335)
Re:Rolling the Dice with CSS Random() by ameliaquining on Sunday August 24, 2025 @08:21PM (#45009045)
And only in a prerelease build; no browser has yet shipped this to users by default.
Re:Rolling the Dice with CSS Random() by JKCalhoun on Sunday August 24, 2025 @08:50AM (#45003881)
Perhaps you can set the seed to a fixed value on page loads? I kind of like the idea of the same "random" star field even if the user refreshes the page. Or rather, it would somewhat bother me if it changed for a refresh since a refresh is supposed to simply re-present the same web page.
Re:Rolling the Dice with CSS Random() by chipsrafferty on Sunday August 24, 2025 @09:09AM (#45004004)
Says who? Why would I refresh to see the same page? Usually I refresh because I want to see some different content.
Re:Rolling the Dice with CSS Random() by Analemma_ on Sunday August 24, 2025 @10:02AM (#45004323)
The starfield example is cool but it seems like that might be exactly where random() wouldn’t work as well as people hope: true randomness often looks pretty bad when you want to make graphics out of it, because true randomness has clumps and voids, and a lot of observers think it looks less random than pseudorandom sequences with more evenly-spaced points.

The term for this is “low-discrepancy sequences”, there have been a handful of HN posts on it over the years. I know I’m bikeshedding the API already before it even really exists, but for image presentation I think a lot of applications might actually find that more useful.

Re:Rolling the Dice with CSS Random() by capitainenemo on Sunday August 24, 2025 @11:20AM (#45004895)
This seems like a good opportunity to bring up the old, more hacky, but also more performant and predictable CSS random effect using backgrounds of prime number sizes to achieve a "random" distribution. The "cicada principle"

https://www.sitepoint.com/the-cicada-principle-and-why-it-ma...

https://lea.verou.me/blog/2020/07/the-cicada-principle-revis...

In this case you would use multiple transparent tiles of different star patterns (images, or gradient/clip-path tricks), each one a different prime number in size. It should work with anything you can tile and overlay in CSS though.

Re:Rolling the Dice with CSS Random() by pstuart on Sunday August 24, 2025 @01:57PM (#45006253)
I'm not sure if I'll ever get a chance to use that but it was very informative nonetheless.
Re:Rolling the Dice with CSS Random() by capitainenemo on Sunday August 24, 2025 @02:22PM (#45006433)
(oh, I should note that the 2nd link uses nth selector to apply any rules pseudo-randomly, not just tiles) ... and, hm, I guess you could "seed" the pseudorandom nth selectors if your pages had unique attribute selectors, by adjusting the primes and offsets. Like with drupal you could do different ones based on digits of the nid in the body tag.
Re:Rolling the Dice with CSS Random() by ericyd on Sunday August 24, 2025 @10:18PM (#45009596)
These examples feel a bit contrived, are there any other cases where random CSS values would be useful? I don't often reach for randomness when doing business apps.
Re:Rolling the Dice with CSS Random() by EduardoBautista on Monday August 25, 2025 @12:14AM (#45010130)
I can only imagine the groundbreaking and innovative MySpace themes that would have been possible with this new random technology.