🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Random numbers play a vital role in statistics, simulations, data analysis, and even in cryptography. Generating random numbers allows us to produce unbiased samples from a dataset or simulate random events. In this guide, we will explore how to generate random numbers in R.
2. Program Overview
The program will:
1. Generate a single random number between 0 and 1.
2. Generate a sequence of five random numbers between 0 and 1.
3. Generate five random integers between 1 and 100.
4. Display the generated random numbers.
3. Code Program
# Generate a single random number between 0 and 1
single_random <- runif(1)
cat("Single random number between 0 and 1:", single_random, "\n")
# Generate a sequence of five random numbers between 0 and 1
sequence_random <- runif(5)
cat("Sequence of five random numbers between 0 and 1:\n", sequence_random, "\n")
# Generate five random integers between 1 and 100
integers_random <- sample(1:100, 5)
cat("Five random integers between 1 and 100:\n", integers_random, "\n")
Output:
Single random number between 0 and 1: 0.4567890 Sequence of five random numbers between 0 and 1: 0.1234567 0.7890123 0.4567890 0.9876543 0.3210987 Five random integers between 1 and 100: 45 78 23 89 67
4. Step By Step Explanation
1. To generate a single random number between 0 and 1, we use the runif() function with an argument of 1. The result is stored in the "single_random" variable.
2. To generate a sequence of random numbers between 0 and 1, again we use the runif() function but this time with an argument of 5 to produce five random numbers. The result is stored in "sequence_random".
3. To generate random integers between specified ranges, we use the sample() function. Here, we generate five random integers between 1 and 100 and store the result in "integers_random".
4. Finally, we display all the generated random numbers using the cat() function.
Note: The values of the generated random numbers will vary each time the program is run.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment