Thanks to visit codestin.com
Credit goes to www.rameshfadatare.com

R Program to Generate Random Numbers

Introduction

Generating random numbers is a common task in various fields such as simulations, random sampling, and statistical modeling. In R, you can generate random numbers using functions like runif() for uniform distribution, rnorm() for normal distribution, and sample() for generating random samples from a specified range. This guide will walk you through writing an R program that generates random numbers based on different distributions.

Problem Statement

Create an R program that:

  • Generates a specified number of random numbers from a uniform distribution.
  • Generates a specified number of random numbers from a normal distribution.
  • Generates a specified number of random integers from a specified range.
  • Displays the generated random numbers.

Example:

  • Input: Number of random numbers: 5; Range: 1-100
  • Output: Random numbers generated using different methods.

Solution Steps

  1. Generate Random Numbers from a Uniform Distribution: Use the runif() function.
  2. Generate Random Numbers from a Normal Distribution: Use the rnorm() function.
  3. Generate Random Integers from a Specified Range: Use the sample() function.
  4. Display the Generated Random Numbers: Use the print() function to display the results.

R Program

# R Program to Generate Random Numbers

# Step 1: Generate random numbers from a uniform distribution
uniform_random_numbers <- runif(5, min = 1, max = 100)

# Step 2: Generate random numbers from a normal distribution
normal_random_numbers <- rnorm(5, mean = 50, sd = 10)

# Step 3: Generate random integers from a specified range
random_integers <- sample(1:100, 5)

# Step 4: Display the generated random numbers
print("Random numbers from a uniform distribution:")
print(uniform_random_numbers)

print("Random numbers from a normal distribution:")
print(normal_random_numbers)

print("Random integers from 1 to 100:")
print(random_integers)

Explanation

Step 1: Generate Random Numbers from a Uniform Distribution

  • The runif() function is used to generate random numbers from a uniform distribution. In this example, 5 random numbers are generated in the range of 1 to 100.
    • runif(n, min, max) generates n random numbers between min and max.

Step 2: Generate Random Numbers from a Normal Distribution

  • The rnorm() function is used to generate random numbers from a normal distribution. In this example, 5 random numbers are generated with a mean of 50 and a standard deviation of 10.
    • rnorm(n, mean, sd) generates n random numbers with the specified mean and sd (standard deviation).

Step 3: Generate Random Integers from a Specified Range

  • The sample() function is used to generate random integers from a specified range. In this example, 5 random integers are selected from the range 1 to 100.
    • sample(x, size) generates size random integers from the vector x.

Step 4: Display the Generated Random Numbers

  • The print() function is used to display the generated random numbers from each method.

Output Example

Example Output:

[1] "Random numbers from a uniform distribution:"
[1] 12.36584 86.74591 43.20102 68.89452 23.35671

[1] "Random numbers from a normal distribution:"
[1] 45.34581 57.12843 63.23894 42.68325 49.67824

[1] "Random integers from 1 to 100:"
[1] 54 21 78 32 85
  • Random numbers from a uniform distribution: These numbers are uniformly distributed between 1 and 100.
  • Random numbers from a normal distribution: These numbers are normally distributed with a mean of 50 and a standard deviation of 10.
  • Random integers from 1 to 100: These are randomly selected integers from the range 1 to 100.

Conclusion

This R program demonstrates how to generate random numbers from different distributions and how to generate random integers from a specified range. Random number generation is essential for simulations, random sampling, and statistical modeling, making this example valuable for anyone learning R programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top