Sunday, January 14, 2018

R Functions - Part 2 - Finding the Random Number

R Functions - Part 2 - Finding the Random Number (& How Long It Took)
Using R Studio

At this site, I got this result from R:

This is the type of model I am looking to generate for my point on a line. Each time it is run, it makes a different line. It is advanced and I don't understand it all. I don't plan to either. Yet I do want to come up with something similar.

First objective is how to generate random numbers.

I found this site helpful for some basic R processes. It explained some of the runif function in R.


runif uses the form runif(#,#,#). The first # tells the program how many numbers to return. The second and third numbers explain the range where the program will randomly pick a number.

This site added to the runif function using the sample function. Similar, the runif will give fractions, the sample will give whole numbers.


sample, uses the form sample(#:#, #). The range of numbers that the program will pull a number from is listed with #:#. The # after the comma determines how many numbers are randomly pulled from the range of numbers.

Second objective is how to determine how many random numbers are needed before I obtain a specific number I am looking for.

I need sample to run multiple times until it randomly pulls a specific number I am looking for. I first found while. I then was led to repeat. I also took note of print to see the outputs. I entered this:

repeat {
x = sample(1:100,1)
print(x)
}

into the program. It then continuously spit out a random number. This is what I wanted, but I need it to stop at a specific number. This code starts with repeat {...} and the stuff in the brackets is what is repeated in the program. x = sample(1:100,1) pulls the random number. print(x) displays the result. So the code repeatedly, pulls and displays a single random whole number between 1 and 100. This code keeps going and going and....

To stop the repeating program, I need an if statement. Such as:

repeat {
x = sample(1:100,1)
print(x)
if (x == 6){
       break
   }
}

The additional  if ... {...} is a part of the code that allows for something to happen. The (x==6) tells the program if this statement, in the case x==6 also described when x is 6, then do what is inside the {...}, in this case break also described as stop repeating the problem. I chose 6 to stop the code, but I could have chosen any number.


In this run of the code, it took 13 random numbers from 1 to 100 until 6 was chosen. I will note, the same number could have been picked twice; so the total numbers during some of the runs is larger than 100, the amount of runs to go through every number. For example, one run of the code took 399 runs before the program selected 6.

The next step is coding something that will count how many times the code ran until it came up with the number 6. I obtained the code form this site. I added the code and obtained the following:

i = 0
repeat {
y = sample(1:100,1)
print(y)
i = i + 1
if (y == 6){
       break
   }
}
print(sprintf("The loop repeated %s times.", i))

An example of the results:
...


The code now counts how many attempts it took to randomly pick 6, when picking a single number between 1 and 100. Now to go into some detail on the code.

i = 0
repeat {
y = sample(1:100,1)
print(y)
i = i + 1
if (y == 6){
       break
   }
}
print(sprintf("The loop repeated %s times.", i))

i=0 is presented before the repeat. i is the counter. Then in the repeat, i = i + 1 added one to the zero. Each time it is repeated, one is added. Once the number is picked, the repeat stops, the code spits out the result of the ones added each run. This gives me the total. print(sprintf("The loop repeated %s times.", i)) is the fancy code to just display the results.

That is enough for now.

Have a nice day!
Comment if you would like.

No comments:

Post a Comment