excel - VBA Randomly generated strings repeating - why? -
i have created vba function produce randomly generated string of format xx00xxx. ran create in excess of 400000 serials in excel spreadsheet, there abnormally high number of repititions of same string; on 60% of strings have more 1 instance (>250 in cases!). expect repitition near level. there problem code? or perhaps week play lottery.
function newvrn() string dim strone string dim strtwo string dim strthree string dim strfour string dim strfive string dim strsix string dim strseven string ' initialize seed using timer. randomize 'generate random letters strone = chr(int((90 - 65 + 1) * rnd) + 65) randomize strtwo = chr(int((90 - 65 + 1) * rnd) + 65) randomize strfive = chr(int((90 - 65 + 1) * rnd) + 65) randomize strsix = chr(int((90 - 65 + 1) * rnd) + 65) randomize strseven = chr(int((90 - 65 + 1) * rnd) + 65) randomize 'generate random numbers strthree = chr(int((57 - 48 + 1) * rnd) + 48) randomize strfour = chr(int((57 - 48 + 1) * rnd) + 48) 'concatenate , build vrn newvrn = strone & strtwo & strthree & strfour & strfive & strsix & strseven end function
the function called part of sub first other 'stuff' , (at point of calling function) writes result cell , selects next cell. in quick succession no other operations between. far can see, serials not repeated on next call.
i've had @ of other solutions on here none seem answer specific problem. accepted there limitations rng in office think observation here quite extreme. there problem modern processors (with several calcultions per timer interval) randomize statements , independent execution of function should have resolved this.
p.s. ideally wanted unique list of random serials - acceptance probability mean there minimal number of repeats. in end created loop add 1 sequence each time (ie aa00aaa, aa00aab etc). still know why random generator not working!
you need call randomize
once, @ beginning of program.
each time call randomize
(without specifying seed) vb using system timer set random number use next.
call randomize
in quick succession , run risk of vb using same random number, you've observed.
Comments
Post a Comment