excel vba - VBA Array issues -
i trying generate random variable 0-1 5000 times. , then, put them ranges e.g. 0-0.05, 0.05-0.1.... count frequency of each range.
however, seems code doesnt work. on it? appreciated!!!!
option explicit option base 1 sub mna() dim iteration long, long iteration = 5000 redim ai1(iteration) double = 1 iteration: cells(4, 3) = ai1(i) = randomnumber next call hist1(iteration, 20, 0, 1, ai1) end sub function randomnumber() randomize randomnumber = rnd() end function sub hist1(n variant, m long, start double, right double, arr() double) dim long, j long, find long dim length double redim breaks(m) single redim freq(m) single = 1 m freq(i) = 0 next length = (right - start) / m = 1 m breaks(i) = start + length * (i) next = 1 n if (arr(i) <= breaks(1)) freq(1) = freq(1) + 1 if (arr(i) >= breaks(m - 1)) freq(m) = freq(m) + 1 j = 2 -1 if (arr(i) > breaks(j - 1) , arr(i) <= breaks(j)) freq(j) = freq(j) + 1 next j next = 1 m cells(3, + 13) = breaks(i) cells(4, + 13) = freq(i) next end sub
this should work. arcadeprecinct said had typo. have put call hist1(iteration, 20, 0, 1, ai1)
inside loop.
hope can learn this.
edit following rodger's comment bellow
option explicit option base 1 sub mna() dim iteration long, long iteration = 5000 redim ai1(iteration) double = 1 iteration: cells(4, 3) = ai1(i) = randomnumber call hist1(iteration, 20, 0, 1, ai1) next end sub function randomnumber() randomize randomnumber = rnd end function sub hist1(n variant, m long, start double, right double, arr() double) dim long, j long, find long dim length double redim breaks(m) single redim freq(m) single = 1 m freq(i) = 0 next length = (right - start) / m = 1 m breaks(i) = start + length * (i) next = 1 n if (arr(i) <= breaks(1)) freq(1) = freq(1) + 1 if (arr(i) > breaks(m - 1)) freq(m) = freq(m) + 1 j = 2 m - 1 if (arr(i) > breaks(j - 1) , arr(i) <= breaks(j)) freq(j) = freq(j) + 1 next j next = 1 m cells(3, + 13) = breaks(i) cells(4, + 13) = freq(i) next end sub
Comments
Post a Comment