![]() ![]() ![]() Just remember that bigger powers greater than one will bias you towards the minimum value, and smaller powers less than one but greater than zero will bias you towards the maximum value. From top to bottom: 1/4, 1/2, 2, 4)Įxperiment with different powers to find what works for you. (example of mapping a 0-1 range by raising x to different powers. Using different values of p can bias you towards the minimum or maximum value depending on your needs: Will give you an answer between 0 and 1 (inclusive), but the result is more likely to be close to one, following a square root curve.Īnd since the min and max of the output are still 0 and 1, this distribution can be mapped to any range (given min, max, and the exponent p) like so: result = min + (max - min) * pow(rand(), p) Will give you an answer between 0 and 1 (inclusive), but the result is more likely to be close to zero, following a quadratic curve. Let's say "rand()" gives you a random number between 0 and 1 (inclusive). You can find methods to get custom-shaped probability distributions in this answer or the links suggested in comments above. (and we can apply the same scale/offset/floor approach to get a corresponding discrete distribution) Using inverse transform sampling you can get this same linear distribution (without the 0 artifact) with one random sample via the formula: 1 - sqrt(1 - random()) If you're using this with floor after the abs to generate random integers, then this little quirk will be swamped and not noticeably affect the result. So you have about half the chance of getting exactly zero as you have of getting the next larger representable number. without the floor): the only way to get zero out of abs is to start with zero, but you can get a value of epsilon if the input is either positive or negative epsilon. Note as pointed out by Logan Pickup in the comments below, there's a slight mathematical artifact here if you're using this to generate continuous random numbers (ie. ![]() Gives you a random number between min and max, with outputs closer to min being more common, falling off linearly toward the max. Peaks around 0 and falls off toward 1 floor(abs(random() - random()) * (1 + max - min) + min) Gives a distribution that peaks at 0 and falls off toward -1 and 1. (I see the edit attempt to "fix" the mismatched bracket above, but this is deliberate and carries specific meaning) random() - random() Assuming you have a random() function that returns a uniformly-distributed numeric value in the interval [0, 1). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |