gpt4 book ai didi

使用集合列表创建数组

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:13 25 4
gpt4 key购买 nike

我有这个,

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

}
word[ n ] = '\0';

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

最佳答案

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

然后使用这个随机整数来索引你的字符数组:

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

请注意,rand() % 5 可能不是 generate a random number in a range 的最佳方式.

关于使用集合列表创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30854028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com