Jump to content


random() can it be seeded?

Recommended Posts

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.




Link to comment
Share on other sites

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.


Link to comment
Share on other sites

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


Link to comment
Share on other sites

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

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

@ZachSaucier is correct - GSAP just uses Math.random(), so unless I'm misunderstanding your goal I don't think seeding is possible. 

  • Thanks 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.