下面是Ruby版本的:
再下面是C版本的:
#Get random alpha string
def alphaList
((0..9).inject([]){|arr,x|arr<<x.to_s}+('a'..'z').to_a+('A'..'Z').to_a)
end
def randChar(alpha,size)
asize=alpha.size
(1..size).inject([]) do |arr,x|
arr<<alpha[(rand*10**(asize.to_s.size)%asize).round]
end.to_s
end
puts randChar(alphaList,100000)
再下面是C版本的:
int i,j;
char randStr[10],rs[1];
char str[64] = "00123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
memset(randStr, 0, sizeof(randStr));
for(i=0;i<10;i++){
j=rand()%64;
sprintf(rs,"%c",str[j]);
strcat(randStr,rs);
}