Share Posted January 13, 2020 Hi finally getting round to updating all my stuff from GSAP2 - 3. Late to the party I know!! Liking the look of some of the utility functions, was wondering can gsap.utils.random() be seeded, didn't see any reference to that in the docs. Thanks Matt Link to comment Share on other sites More sharing options...
Share Posted January 13, 2020 Hey macguffin. Being late to the party is better than never going to the party! You could use the random with an ease function. And even use pipe to make it clean! In fact, it's already one of our helper functions // reusable function. Feed in an array and an ease and it'll return // a function that pulls a random element from that array, weighted // according to the ease you provide. function weightedRandom(collection, ease) { return gsap.utils.pipe( Math.random, //random number between 0 and 1 gsap.parseEase(ease), //apply the ease gsap.utils.mapRange(0, 1, -0.5, collection.length-0.5), //map to the index range of the array, stretched by 0.5 each direction because we'll round and want to keep distribution (otherwise linear distribution would be center-weighted slightly) gsap.utils.snap(1), //snap to the closest integer i => collection[i] //return that element from the array ); } // usage: var myArray = [0, 1, 2, 3], getRandom = weightedRandom(myArray, "power4"); // now you can call it anytime and it'll pull a random element from myArray, weighted toward the end. getRandom(); getRandom(); ... Link to comment Share on other sites More sharing options...
Author Share Posted January 13, 2020 Thanks Zach, Would that be the same as having a seeded random number? I need to be able to have a random function that whilst random can always reproduce the same "randomness" if the seed is the same. For example if I have this array [a,b,c,d,e,f,g] and need to pick 3 random elements: If I seeded your random function with "1" I would always pick d,e,f If I then seeded it with "4" I would always pick a,f,g and so on When I build games I have to add randomness, but the game needs to be recreatable. I currently use a cut down version of https://chancejs.com/ but if GSAP was able to do this that would be awesome. Thanks for your help Matt Link to comment Share on other sites More sharing options...
Share Posted January 13, 2020 Ah, sorry. I was misunderstanding you. GSAP's random makes use of JS's Math.random() internally so I don't think it can be seeded by default, though I may be incorrect. But you could build seeding functionality around it. If the options are limited like your example it'd be easy to make a map of possible options. @GreenSock or someone else in the forums may be able to provide additional insight regarding other methods of seeding it. Link to comment Share on other sites More sharing options...
Author Share Posted January 13, 2020 Ahh ok if it's just using Math.random() I think you're right and it cannot be seeded. Link to comment Share on other sites More sharing options...
Share Posted January 13, 2020 @ZachSaucier is correct - GSAP just uses Math.random(), so unless I'm misunderstanding your goal I don't think seeding is possible. 1 Link to comment Share on other sites More sharing options...
Share Posted January 13, 2020 Having a prng would complicate the random api a lot. Here's a good library for that. https://github.com/davidbau/seedrandom And some videos showing how to make your own. 3 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now