R Programming: Generate 10 Random integers between -50 and +50
R Programming: Basic Exercise-4 with Solution
Write a R program to create a vector which contains 10 random integer values between -50 and +50.
Sample Solution :
R Programming Code :
# Generate a vector 'v' with 10 random integer values between -50 and +50
# The 'replace=TRUE' parameter allows sampling with replacement
v = sample(-50:50, 10, replace=TRUE)
# Print a message indicating that the content of the vector will be shown
print("Content of the vector:")
# Print a message specifying that 10 random integer values between -50 and +50 will be shown
print("10 random integer values between -50 and +50:")
# Print the vector 'v' to display the random integer values
print(v)
Output:
[1] "Content of the vector:" [1] "10 random integer values between -50 and +50:" [1] 31 -13 -21 42 49 -39 20 12 39 -2
Explanation:
- Generate Random Integers:
- v = sample(-50:50, 10, replace=TRUE) creates a vector v with 10 random integers between -50 and +50. The replace=TRUE argument allows for repeated values.
- Print Vector Label:
- print("Content of the vector:") outputs a label indicating that the following output will show the content of the vector.
- Print Description:
- print("10 random integer values between -50 and +50:") provides a description of the values that will be displayed.
- Print the Vector:
- print(v) displays the vector of 10 random integers.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to create a sequence of numbers from 20 to 50 and find the mean of numbers from 20 to 60 and sum of numbers from 51 to 91.
Next: Write a R program to get the first 10 Fibonacci numbers.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/r-programming-exercises/basic/r-programming-basic-exercise-4.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics