array of randomly generated names
Steven Schveighoffer
schveiguy at yahoo.com
Sat Oct 16 09:32:03 PDT 2010
On Fri, 15 Oct 2010 15:50:53 -0400, spir <denis.spir at gmail.com> wrote:
> Hello,
>
> A few questions raised by a single func.
>
> ===================
> alias char[] Text ;
>
> Text letters = ['a','b','c',...] ;
>
> Text[] nameSet (uint count , uint size) {
> /* set of count random names of size size */
> Text[] names ; names.length = count ;
> Text name ; name.length = size ;
> for (int i=0 ; i<count ; i++) {
> for (int j=0 ; j<size ; j++)
> name[j] = letters[uniform(0u,26u)] ;
> names[i].length = size ;
> names[i][] = name ;
> }
> return names ;
> }
> ===================
>
> 1. In the inner loop generating name, I have found neither a way to
> feed directly ints into name, nore a way to cast ints to chars using to!
> (also found no chr()). So, I had to list letters. But this wouldn't work
> with a wide range of unicode chars... How to build name directly from
> random ints?
Can't you just use addition?
i.e. i = 0-25 mapped to letters a-z is cast(char)('a' + i)
If you are going for unicode, I'd suggest using wchar or dchar (which
could be directly cast from integers) to build the string, and then use
to!(char[])(widename);
Finally, I'd suggest accepting a dchar[] letters as a third optional
parameter in case someone wants names in a specific language.
-Steve
More information about the Digitalmars-d-learn
mailing list