How to generate skewed random numbers in javascript?

Using Javascript, how can I generate random numbers that are distorted towards one or the other distribution? Or, ideally, a point within a range?

In context: I am creating a user interface that uses a random gray square grid. I generate a gray RGB value using Math.random() , but I would like it to skew grays on average darker or lighter, but still have a full range from black to white.

(I think this is a similar question for Conquering java random number generation to a specific number , but I'm working with Javascript ...)

Any help is greatly appreciated.

+4
source share
3 answers

Raise Math.random() to power to get a gamma curve - this changes the distribution between 0 and 1, but 0 and 1 endpoints remain constant.

 var r= Math.pow(Math.random(), 2); var colour= 'rgb('+r*255+', '+r*255+', '+r*255+')'; 

For gamma> 1 you will get a darker output; for 0 <gamma <1 you become easier. (Here β€œ2” gives the x-square curve, equidistant lightness will be β€œ0.5” for the square root curve.)

+11
source

It seems a bit rude and less elegant than @bobince's answer, but what the hell.

 //setup var colours = [], num_colours = 10, skew_to = 255, skew_chance = 20; //get as many RGB vals as required for (var i=0; i<num_colours; i++) { //generate random grey var this_grey = Math.floor(Math.random() * 256); //skew it towards the @skew_to endpoint, or leave as-is? if (Math.floor(Math.random() * 100) >= skew_chance && this_grey != skew_to) { //skew by random amount (0 - difference between curr val and endpoint) var skew_amount = Math.floor(Math.random() * Math.abs(this_grey - skew_to)); this_grey += ' (skewed to '+(skew_to < this_grey ? this_grey - skew_amount : this_grey + skew_amount)+')'; } colours.push(this_grey); } console.log(colours); 

Essentially, it generates random gray dots, and then decides, based on the probably specified (in percent) skew_chance , skew_chance it or not. (If you want to make it random, not permanent). If he decides to skew, a random number is then added or subtracted from the gray value (depending on whether the end skew point is lower or higher than the current value).

This random number is a number from 0 to the absolute difference between the current value and the end point, for example. if the current value is 40 and the endpoint is 100, the added number will be between 0 and 60.

Like I said, @bobince's answer is somewhat, er, more elegant!

+2
source

[This may be a slightly different approach.]

This approach deals with getting the number as follows:

 random = numberToSkewTo + random(-1,1)*stdDeviation 

Where:

  • numberToSkewTo is the number you want to skew.
  • stdDeviation - deviation from numberToSkewTo
  • numberToSkewTo + abs(stdDeviation) <= MAX_NUMBER and
  • numberToSkewTo - abs(stdDeviation) >= MIN_NUMBER

What the following code does, it selects a random number around a given number with constantly increasing standard deviations. It returns the average of the results.

 function skew(skewTo,stdDev){ var rand = (Math.random()*2 - 1) + (Math.random()*2 - 1) + (Math.random()*2 - 1); return skewTo + rand*stdDev; } function getRandom(skewTo){ var difference = Math.min(skewTo-MIN_NUMBER, MAX_NUMBER-skewTo); var steps = 5; var total = 0.0; for(var i=1; i<=steps; i++) total += skew(skewTo, 1.0*i*difference/steps); return total/steps } 
+1
source

All Articles