-
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.
Last edited by kavelle; 04-25-2004 at 03:53 AM.
-
Senior Member
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
Last edited by jbum; 04-25-2004 at 04:08 AM.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|