ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Control the number of Decimal points of a random number with a slider control

FAQ   •   VIEW ALL Control the number of Decimal points of a random number with a slider control on Mar 4, 2019 at 1:20:27 pm

Im using

seedRandom(index,true)
random(0,100)

to create a random number, is there a way to control the number of decimal points the random number outputs with a slider control?

I thought linking a slider using .toFixed(x) would work but it doesn't

Anyone?

 Return to posts index Re: Control the number of Decimal points of a random number with a slider controlon Mar 4, 2019 at 1:34:51 pm

I managed to figure it out.

2 sliders one called value which generates the number with this in the source

seedRandom(index, true)
random(0,100)

and another slider with the code

```places = effect("Number of Decimals")("Slider"); val = effect("Value")("Slider"); //sub in the name of your slider here factor = Math.pow(0.1, places) ; sign = ""; if (val &lt; 0) sign = "-"; val = Math.abs(val); whole = Math.floor(val); fraction = "" + Math.round((val - whole)/factor)*factor; for (fraction; fraction.length &lt; places+2; fraction+="0"); sign + whole + "." + fraction.substring(2, places+2) ```

 Return to posts index Re: Control the number of Decimal points of a random number with a slider controlon Mar 4, 2019 at 3:51:59 pm

Your code is rather complicated. If you have a random number and want to round it to x decimals, just go
``` var myNumber = someRandomNumber; var multiplicator = Math.pow(10,x); Math.round(myNumber*multiplicator)/multiplicator; ```

To simple things up, multiplicator is 10 if you want a decimat, 100 if you want 2 etc.

Andrei
My Envato portfolio.

 Return to posts index Re: Control the number of Decimal points of a random number with a slider controlon Mar 6, 2019 at 9:35:33 amLast Edited By Kalleheikki Kannisto on Mar 6, 2019 at 9:38:15 am

Or
`parseFloat(random(100)).toFixed(Math.round(*pick-whip slider value here*));`

Kalleheikki Kannisto
Senior Graphic Designer

 Return to posts index