Jump to content
Search Community

gsap.utils.random incorrect values

chrisgannon test
Moderator Tag

Recommended Posts

I've either misunderstood what gsap.utils.random does or it's spitting out the wrong values.

 

Click the pen and a random value will display. My understanding was that it will choose values between 0 and 100,000 (in this case).

 

But it actually displays negative values too. If I put a snapping number in, like 1, (gsap.utils.random(0, 100000, 1)) it picks values from the correct range (but I don't get decimals).

 

See the Pen bb8db93f1783b4b0f0d3ac5d55f11f9a by chrisgannon (@chrisgannon) on CodePen

  • Thanks 1
Link to comment
Share on other sites

This should be resolved in the next release which you can preview at https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/gsap-latest-beta.min.js

 

The issue was that for performance reasons, I was using a bitwise ~~ instead of Math.floor(), but the bitwise operator doesn't handle huge values. Check it out in the console - 

 

// works (including any number below this)
~~1000000000.25 // 1000000000

// add one more zero and it doesn't:
~~10000000000.25 // 1410065408

// add a few more and it flips negative: 
~~1000000000000.25 // -727379968

That's why it only broke when you fed in a very large number. And yes, it was less than the one I show above, but for snapping, I must multiply that by a factor first which adds some zeros. 

 

Does that clear things up? Sorry about the confusion/hassle there. I just used Math.floor() now in the next release. 

  • Like 3
Link to comment
Share on other sites

5 minutes ago, GreenSock said:

The issue was that for performance reasons, I was using a bitwise ~~ instead of Math.floor(), but the bitwise operator doesn't handle huge values

 

I thought I brought this up before. I guess this fixes it too.

 

 

 

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.
×
×
  • Create New...