How can I generate a random string in Matlab?

前端 未结 4 2054
北荒
北荒 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条回答
  •  旧时难觅i
    2020-12-15 22:16

    This should work

    s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    
    %find number of random characters to choose from
    numRands = length(s); 
    
    %specify length of random string to generate
    sLength = 50;
    
    %generate random string
    randString = s( ceil(rand(1,sLength)*numRands) )
    

提交回复
热议问题