Jump to content


random value is a string

Recommended Posts

When using "random()", the value ends up as a string. I would expect it's type to stay the same. This can cause problems with code that is expecting the value to be a number.





See the Pen 58964daaa90343ed392c30ed9bffe9dc by osublake (@osublake) on CodePen

Link to comment
Share on other sites

The string-based syntax you're using SHOULD result in it being a string because the random number substitution inside strings like that is specifically for strings. In other words, after the swap, it'd look like: gsap.to(obj, {x: "10.2347"}), for example. And GSAP does its best to honor what you're telling it to tween to, and in this case you're literally telling it that you want the end to be a string of "10.2347". See what I mean? 


If you want a number, just use gsap.utils.random()

gsap.set(obj, {
  x: gsap.utils.random(0, 100),


Link to comment
Share on other sites

Ha - good catch. Such an odd edge case. It would only switch to a string if the start and end value had a decimal because of a check I was doing to sense if we could use the fast/simple numeric interpolation instead of complex string interpolation: 

if (!isNaN(start + end)) {


So if both had decimals and at least one of them was a string, it'd concatenate it as a string and end up with a value like "1.2345.147" which isNaN. Kinda funny. I just had to switch "+" to "*" to resolve that. It's fixed in the next release which you can preview at https://assets.codepen.io/16327/gsap-latest-beta.min.js


And yeah, I misspoke - it will try to keep it as a number if possible rather than going to a string. 


As for the string-based "random(...)" in the docs, it's on all the most popular tween pages like .to(), .from(), and .fromTo()

https://greensock.com/docs/v3/GSAP/gsap.to() (scroll down to the "random values" section)

  • Like 3
Link to comment
Share on other sites

I added a note about the string form in the utility method docs to help people be aware of it.

  • Like 2
  • 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.