To generate standard normal random numbers in R, use the “rnorm()” function.
Example 1: Basic usage
set.seed(99)
normal_dist_random_nums <- rnorm(10)
print(normal_dist_random_nums)
Output
[1] 0.2139625 0.4796581 0.0878287 0.4438585 -0.3628379 0.1226740
[7] -0.8638452 0.4896243 -0.3641169 -1.2942420
Example 2: Providing mean and sd
In this function, mean and sd are optional arguments you can specify based on your requirements.
set.seed(99)
normal_dist_random_nums <- rnorm(900, mean = 30, sd = 60)
hist(normal_dist_random_nums)
Output
[1] 44.27925 49.59316 41.75657 48.87717 32.74324 42.45348 22.72310 49.79249
[9] 32.71766 14.11516
Example 3: Creating histogram
set.seed(99)
normal_dist_random_nums <- rnorm(10, mean = 40, sd = 20)
hist(normal_dist_random_nums)
Output
I have used RStudio to show the plot using the hist() function.
Let’s see another example.
set.seed(99)
normal_dist_random_nums <- rnorm(900, mean = 30, sd = 60)
hist(normal_dist_random_nums)
Output
That’s it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.