A Flash Developer Resource Site

1. ## curved /scewed random distribution

How do I get a curved/scewed random distribution?

ie, if I wanted a random number between 0 and 100, but the lower a number is the more likely it is to show up. So 100 would show up very few times, and 1 would show up more often.

Anyone know the best way to do this? I have only come up with cumbersome ways of doing this.

2. This method uses a power curve.

power = 2;
r = Math.floor(Math.pow(Math.random(),power)*100);

I'm using Math.random() to get a value from 0-.999999

Then I'm multiplying it by itself.
Math.pow(r,2) is the same as r*r (r to the second power).

Note that .9*.9 is pretty close to .9.9, but .5*.5 is .25 and .1*.1 is a very small number. So the curve makes the number weight towards zero. Then we multiply that number by 100 and get the integer value.

If power is 1, then you'll get regular linear distribution. To change the distribution, use different powers (e.g. 1.5 or 3).

If you find a value of 'power' that you like, you can just use it as a literal, rather than assigning it to the power variable.

For example:

r = Math.floor(Math.pow(Math.random(),2)*100);

Also note that this method returns 100 unique numbers (from 0-99). If you really want it to go from 0-100, then use 101 instead of 100. Do not use Math.round(), as it will screw up your distribution (you'll get fewer zeros).

It's also worth mentioning that I tend to figure stuff like this out by playing with Excel (or whatever spreadsheet I happen to have). It helps to make graphs to see what kinds of curves I'm getting from these equations.

- Jim

3. thats utterly fantastic. Thankyou very much, you have been a great help!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap