阅读背景:

使用集合列表创建数组

来源:互联网 

I have this,

我有这个,

    srand( ( unsigned) 0 );
    for( n = 0; n < len; ++n )  
     {                          
         word[ n ] = 'a' + rand( ) % ;

     }
     word[ n ] = '

I have this,

我有这个,

    srand( ( unsigned) 0 );
    for( n = 0; n < len; ++n )  
     {                          
         word[ n ] = 'a' + rand( ) % ;

     }
     word[ n ] = '\0';

and i need it to select the random characters from a set list of non consecutive, characters, B,H,G,K,V and fill the string with the characters selected. What I don't know how to do is get my program to select from only those few characters

我需要它从非连续,字符,B,H,G,K,V的集合列表中选择随机字符,并用所选字符填充字符串。我不知道怎么办是让我的程序只从那几个字符中选择

1 个解决方案

#1


2  

Generate an integer between 0 and 4 (last index of [B,H,G,K,V]).

生成0到4之间的整数([B,H,G,K,V]的最后一个索引)。

Then use this random integer to index into an array of your characters:

然后使用此随机整数索引到您的字符数组:

static const char letters[] = "BHGKV"; //your letters as a string
word[n] = letters[rand() % 5]; //get a random letter from letters

Note, rand() % 5 may not be the best way to generate a random number in a range.

注意,rand()%5可能不是在范围内生成随机数的最佳方法。


'; srand( ( unsigned)



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: