Generates a random number from a normal distribution using rnorm() & If – else statement in R Programming

1–2 minutes

read

This R program involves an if-else statement to check a condition:

  • rm(number): This command removes any existing object named ” number” from the R environment.
  • random_numbers <- rnorm(1): A single random number is generated from a standard normal distribution using the rnorm() function and stored in the variable random_numbers.
  • if(random_numbers > 1){: This line starts an if statement. It checks whether the value stored in random_numbers is greater than 1.
  • If the condition in the if statement is true:
  • number <- "Number is greater than 1": The variable number is assigned the string value “Number is greater than 1”.
  • If the condition in the if statement is false:
    • number <- "Number is less than 1": The else statement executes, and the variable number is assigned the string value “Number is less than 1”.

Leave a comment