Question

I got this answer earlier today for how to distort a random number seed toward one bound of the range:

var random = Math.pow(Math.random(), 2);

But that obviously distorts it along an exponential curve.. How can I make it linear?

Also, kind of relevant: I just created this simple script to visualize different types of distributions. It may be helpful for this question: http://jsfiddle.net/RTbrL/

Was it helpful?

Solution

It sounds like you are describing a one-sided triangular distribution.

If so, try

var random = 1.0 - Math.sqrt(Math.random());

or

var random = Math.abs(Math.random() - Math.random());

OTHER TIPS

Like so?

var rand = Math.ceil( Math.random() * 5 ) % 5;

rand goes from 0 to 4.

% in Javascript is Modulus

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top