Drawing a ball from a bag

I am learning to program. Here is an example in R of simulating the problem of drawing 1 ball from a bucket of 200 balls, and repeating this a total of 300 times. How many times does the same ball come up in succession? Obviously depends.

Simulate the drawing of the balls

lotto <- floor(runif(300,min=1,max=200))
[1] 182 26 167 54 173 121 86 27 105 128 14 169 56 97 73 117 47 118 3 133 38 84 130 149 165 153 43 166 159 60 120 40 66 180 2 14 170 45 193 141 123 163 171 1
[45] 39 52 193 25 19 76 21 139 192 123 118 164 21 45 88 193 129 4 47 50 14 186 125 108 122 83 141 73 6 16 26 62 90 11 94 188 36 144 183 198 28 142 119 184
[89] 32 156 62 136 158 40 140 6 157 121 142 8 56 15 84 16 63 4 39 172 174 15 117 139 171 158 15 62 156 139 1 170 115 5 162 136 138 43 114 9 100 54 85 139
[133] 165 5 109 29 59 51 103 142 40 73 65 127 173 131 11 51 87 20 11 146 139 83 110 34 198 106 110 112 70 122 182 123 101 54 116 168 66 96 41 98 108 130 126 113
[177] 113 106 173 150 60 27 192 79 35 173 183 57 107 21 182 138 15 188 71 93 127 185 2 67 83 169 157 51 113 55 131 17 17 33 33 97 62 14 60 11 147 118 124 131
[221] 4 190 111 173 96 176 152 177 107 111 137 107 169 60 163 8 95 70 41 60 96 48 124 81 161 52 143 95 117 167 187 146 167 14 72 101 136 57 21 150 190 180 199 37
[265] 199 188 61 164 123 168 115 29 20 46 90 181 47 7 24 19 120 32 76 84 122 80 132 193 153 32 150 174 47 151 97 27 134 190 183 149

Next search through to see if one element is the same as the next one.

j=0  # the number of successive elements that are the same. Starts at 1 and will increase by 1 every time a pair is found.
for (x in 1:299) { # simple for loop. Only need to go to 299 as comparing the xth value with x+1th
if(lotto[x] == lotto[x+1]) { #remember double == sign. This is a simple logic statement in r
j=j+1 #if they are the same we increase the number of found pairs by 1
cat("lotto[",x,"]=",lotto[x],"\n") #this prints the value of x and the xth element of the vector lotto. As well as starting a new line.
}
}
print(j)

This gives the output

lotto[ 176 ]= 113
lotto[ 208 ]= 17
lotto[ 210 ]= 33
print(j)
[1] 3

So 3 times there are the same number in succession at points 176 and 177, 208 and 209, 210 and 211.

The expected number of times would be 1.5.

Another go producing a different vector had only one occurence

124 60