Please write the following R code setseed222 x sample11001
Please write the following R code: set.seed(222) x = sample(1:100,1) vec = c(4,5,8,11,x) Now, what is the sum of all elements in the vector variable vec?
Solution
set.seed(222) #this function makes the generated random values to be same whenever you run this
x = sample(1:100,1) #sample function return 1 number in the range 1 to 100;here 1 represents size of output
vec = c(4,5,8,11,x)
print(sum(vec))# this function prints the sum of elements of vec.
output:
[1]122
--->you will get the same result, every time you run the program.
this is because we used the set.seed(222). this will make the same sample to be generated every time you run.
so x will have the same value in each and every run (here it is 94);
so when you want to generate random nymbers on purpose you should not use set.seed(seed) function .
So if we write our code this way, the output will be random in each run
x = sample(1:100,1)
vec = c(4,5,8,11,x)
print(sum(v))
