Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matlab random string

Status
Not open for further replies.

manolakis

Computer
Nov 28, 2007
4
0
0
GB
Hi there

I would like to ask if there is any random string generator function in Matlab.

Thanks for your time
 
Replies continue below

Recommended for you

Hey Attached is my RandomString(length) function. As you can see it requires a length as input and then outputs the required result.

If you want to add more letters of characters to the list of what can be generated you are free to (as long as the number of characters doesn't exceed 100 the script will still work).

David
 
 http://files.engineering.com/getfile.aspx?folder=03576db5-a47b-47da-a30d-0eed47fe3e07&file=RandomString.m
Now that is what I call bad Matlab programming!

Try these functions:
The first one generates a random string where repetitions are allowed. The second one generates a random string where repetitions are not allowed.
Code:
function String = RandomString1(n)

% generates a random string of lower case letters of length n

LetterStore = char(97:122); % string containing all allowable letters (in this case lower case only)
String = LetterStore(ceil(length(LetterStore).*rand(1,n)));

Code:
function String = RandomString2(n)

% generates a random string of lower case letters of length n
% n must be less than the number of possible letters
% elements of the returned string are exclusive

LetterStore = char(97:122); % string containing all allowable letters (in this case lower case only)
Idx = randperm(length(LetterStore));
String = LetterStore(Idx(1:n));

M




--
Dr Michael F Platten
 
All I can say then IrishDave is you have only scratched the surface of a really neat bit of software. Many people across the World waste hours trying to out-do each other in squeezing a solution into as few lines as possible (Matlab "golf"). Had this thread been in the Matlab newsgroup your solution would have been mercilessly mocked and MikeyP's would have appeared almost immediately in all sorts of similar forms by the regular golfers there.

Want to learn all the tricks? Frequent that group as well as this site. The group's FAQ is good too.
 
I do like Matlab because it allows me to test things in a simple programming language but my main domain is fluid simulations so using Matlab just doesn't work because of the size of my fields, it quickly just isn't quick enough when compared with C++.

I can understand that my solution wasn't the most Matlabish in style, but it still worked and it's not bad programming, just not using the Matlab functions. :)

David
 
Status
Not open for further replies.
Back
Top