Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
macguffin

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.

 

Thanks

Matt

Share this post


Link to post
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.
getRandom(); 
getRandom(); 
...

 

Share this post


Link to post
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

Matt

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

Ahh ok if it's just using Math.random() I think you're right and it cannot be seeded.

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×