slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.95k stars 568 forks source link

Math.random() #6017

Closed NigelBreslaw closed 2 weeks ago

NigelBreslaw commented 3 weeks ago

Multiple UI possibilities

Common api? Math.random() -> float between 0 and 1. Math.random(10.0) -> float between 0 and 10. Math.random(10) -> Int between 0 and 10. But also do some things based on the type? 20.random() -> float or int between 0 and 20. color.random() -> random color? For example wanting to randomly place star elements to make a stary sky. Seed certain animation values so things don't feel too robotic. Create random colors e.g. rgb(random(256), andom(256), andom(256))

But Math.random() -> float between 0 and 1. would be enough to self create the rest for now with a tiny bit of extra code in the slint file e.g. 100 * random() and also maybe I can even take that task?

ogoffart commented 3 weeks ago

I'm not sure this is a great idea because property binding are supposed to be lazy and pure. They can be re-evaluated any time and should lead to the same result. Maybe if we pass the seed to the function, then that would fit the purpose?

Otherwise, you can implement a pseudo random function yourself.

   // return a number between 0 and 1
   function random(seed: int) -> float {
    return (115249  * (seed + 196) * seed).mod(25117) / 25117;
   }

(there are probably better ones)

NigelBreslaw commented 2 weeks ago

I'm closing this. I'm certain at some point it will be useful to have some kind of random as it's useful for many UI effects. However as part of the Math namespace it would be the only impure function and lead to people like me accidentally using it in a binding because all the others are fine.