Open In App

p5.js randomGaussian() Function

Last Updated : 23 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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() { 
    
    // Creating Canvas size
    createCanvas(550, 140); 
        
    // Set the background color 
    background(220); 
     
    // Calling to randomSeed() function
    // It is used for getting constant random
    // values each time the code is run
    randomSeed(9)
       
    // Calling to randomGaussian() function
    // with mean and sd parameters
    let A = randomGaussian(1, 2);
    let B = randomGaussian(0, 1);
    let C = randomGaussian(2);
    let D = randomGaussian(2, 10);
       
    // Set the size of text 
    textSize(16); 
        
    // Set the text color 
    fill(color('red')); 
      
    // Getting random number
    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() { 
   
    // Creating Canvas size
    createCanvas(550, 140); 
       
    // Set the background color 
    background(220); 
    
    // Calling to randomSeed() function
    // It is used for getting constant random
    // values each time the code is run
    randomSeed(9)
      
    // Calling to randomGaussian() function with
    // mean and sd parameters
    let A = randomGaussian();
    let B = randomGaussian(2.5);
    let C = randomGaussian(2);
    let D = randomGaussian(20, 22.5);
      
    // Set the size of text 
    textSize(16); 
       
    // Set the text color 
    fill(color('red')); 
     
    // Getting random number
    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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads