Code covered by the BSD License  

Highlights from
RANDWORD

Be the first to rate this file! 3 Downloads (last 30 days) File Size: 1.87 KB File ID: #24730

RANDWORD

by Thomas

 

14 Jul 2009 (Updated 05 Feb 2010)

Generate a random word from an alphabet

| Watch this File

File Information
Description

RANDWORD generates a random word from an alphabet. The words are selected from the lexicographically ordered list of all words of the given length using a random index generated with 'rand'.

    WORD = RANDWORD(alpha, wordSize) returns a random word in
    the alphabet 'alpha', with the number of symbols 'wordSize'.
    The result WORD has size 1 x wordSize.

    WORD = RANDWORD(alpha, wordSize, numWords) returns
    'numWords' random words in the alphabet 'alpha', with the
     number of symbols 'wordSize'. The result WORD has size
     numWords x wordSize.

    Example:
    Generate 4 words of length 5 from the alphabet 'A', 'B', ..., 'Z'
       word = randword('A':'Z', 5, 4)
       word =
       VPQQT
       PFQPQ
       OHPYM
       XWAPM

    The method of randword is to select a random word from the
    lexicographically sorted list of all words on alpha of length
    wordSize. With n = length(alpha) and w = wordSize, the steps
    are
       1) Get a random number r on the interval 1:n^w.
       2) Compute base n expansion of r.
       3) Make word with symbols of 'alpha' indexed by
           base n digits of r.

   Author: Tom Richardson
           trichardson AT xrite DOT com
   Date: 14 July 2009
       
   Changed the routine to use multiple passes to ensure that values of
   lexIndex stay below 2^31 - 1, thus avoiding roundoff error in
   alphaSize.
   Modified: Tom Richardson
   Date: 21 January 2010

MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
09 Feb 2010 Jos (10584)

How is this different from the more simple approach:

idx = ceil(numel(alpha)*rand(n,w)) ;
words = alpha(idx)?

Note that the variable "word" is growing inside the lloop, making this algorithm potentially very slow.

A final and positive comment: the help is quite adequate with a description of the in- and outputs, the algorithm and an example. A "See Also" line, referencing RAND and RANDSAMPLE would improve it even further.

Please login to add a comment or rating.
Updates
05 Feb 2010

   Changed the routine to use multiple passes to ensure that values of
   lexIndex stay below 2^31 - 1, thus avoiding roundoff error in
   alphaSize.
   Modified: Tom Richardson
   Date: 21 January 2010

Tag Activity for this File
Tag Applied By Date/Time
random Thomas 15 Jul 2009 10:57:25
word Thomas 15 Jul 2009 10:57:25
lexicographic Thomas 15 Jul 2009 10:57:25
alphabet Thomas 15 Jul 2009 10:57:25

Contact us at files@mathworks.com