p5.js randomGaussian() Function
Last Updated :
23 Aug, 2023
The randomGaussian() function in p5.js is used to return a random value fitting a Gaussian or normal distribution for which mean and standard deviation are given as the parameter.
Syntax:
randomGaussian( Mean, StandardDeviation )
Parameters: This function accepts two parameters as mentioned above and described below:
- Mean: It is the mean of generated random value.
- StandardDeviation: It is the Standard Deviation of generated random value.
Note:
- If one argument is passed as the parameter it means that the mean and standard deviation is 1.
- If two argument is passed as the parameter it means that first is mean and second is a standard deviation.
- If no argument is passed as the parameter it means that mean is 0 and the standard deviation is 1.
Return Value: It returns a random number.
Below programs illustrate the randomGaussain() function in p5.js:
Example 1: This example uses randomGaussian() function return the random value for which mean and standard deviation is given as the parameter.
function setup() {
createCanvas(550, 140);
background(220);
randomSeed(9)
let A = randomGaussian(1, 2);
let B = randomGaussian(0, 1);
let C = randomGaussian(2);
let D = randomGaussian(2, 10);
textSize(16);
fill(color( 'red' ));
text( "Random number is: " + A, 50, 30);
text( "Random number is: " + B, 50, 60);
text( "Random number is: " + C, 50, 90);
text( "Random number is: " + D, 50, 110);
}
|
Output:
Note: In the above example, the variable “C” contains one parameter i.e. mean value is 2 but the standard deviation is 1.
Example 2: This example uses randomGaussian() function return the random value for which mean and standard deviation is given as the parameter.
function setup() {
createCanvas(550, 140);
background(220);
randomSeed(9)
let A = randomGaussian();
let B = randomGaussian(2.5);
let C = randomGaussian(2);
let D = randomGaussian(20, 22.5);
textSize(16);
fill(color( 'red' ));
text( "Random number is: " + A, 50, 30);
text( "Random number is: " + B, 50, 60);
text( "Random number is: " + C, 50, 90);
text( "Random number is: " + D, 50, 110);
}
|
Output:
Reference: https://p5js.org/reference/#/p5/randomGaussian
Share your thoughts in the comments
Please Login to comment...