randomword.m 756 B

1234567891011121314151617181920212223242526272829303132333435
  1. function f = randomword(n,mode)
  2. % function f = randomword(n,mode)
  3. %
  4. % <n> is the desired length of the word
  5. % <mode> (optional) is
  6. % 0 means choose from capital letters
  7. % 1 means choose from capital and lowercase letters and digits
  8. % 2 means choose from capital and lowercase letters
  9. % 3 means choose from lowercase letters
  10. % default: 0.
  11. %
  12. % return a random word consisting of <n> characters.
  13. %
  14. % example:
  15. % randomword(5)
  16. % input
  17. if ~exist('mode','var') || isempty(mode)
  18. mode = 0;
  19. end
  20. % do it
  21. switch mode
  22. case 0
  23. rng = [64+(1:26)];
  24. case 1
  25. rng = [64+(1:26) 96+(1:26) 47+(1:10)];
  26. case 2
  27. rng = [64+(1:26) 96+(1:26)];
  28. case 3
  29. rng = [96+(1:26)];
  30. end
  31. %f = char(rng(randint(1,n,[1 length(rng)])));
  32. f = char(rng(ceil(rand(1,n)*length(rng))));