Jump to content

GreenSock Docs


Returns : *

Returns a random number within a range, random item from an array, or returns a function to return those things depending on what you give it.

Gets a random number within a range (optionally rounding to an increment you provide), or choose a random item in an array. Note that if you add true as the 4th parameter, it tells gsap.utils.random() to return a function that you can call anytime to get a random value according to all the parameters you defined.

  1. //get a random number between -100 and 100
  2. var num = gsap.utils.random(-100, 100);
  3. //get a random value from an array (in this case we'll get "red", "blue", or "green")
  4. var random = gsap.utils.random(["red", "blue", "green"]);
  5. //get a random number between -100 and 100 that's snapped to the closest increment of 5
  6. var num = gsap.utils.random(-100, 100, 5);
  7. //use the 4th parameter (boolean) to get a function back instead of a number
  8. var randomizer = gsap.utils.random(-100, 100, 5, true);
  9. //now we just call that randomizer function from the line above and it'll always return a number between -100 and 100 rounded to the closest increment of 5!
  10. var num = randomizer();
Copyright 2017, GreenSock. All rights reserved. This work is subject to theterms of useor for Club GreenSock members, the software agreement that was issued with the membership.