How can I generate a random string in Matlab?

前端 未结 4 2061
北荒
北荒 2020-12-15 21:56

I want to create a string with random length and random characters, only [a-z][A-Z] and numbers. Is there something built in?

Thanks in advance.

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 22:15

    There isn't much to add to the answers of @Phonon and @dantswain, except that the range of [a-Z],[A-Z] can be generated in less painful way, and randi can be used to create integer random values.

     symbols = ['a':'z' 'A':'Z' '0':'9'];
     MAX_ST_LENGTH = 50;
     stLength = randi(MAX_ST_LENGTH);
     nums = randi(numel(symbols),[1 stLength]);
     st = symbols (nums);
    

提交回复
热议问题