C 编程中从单词列表中生成随机单词

发布于 2024-09-14 13:04:15 字数 591 浏览 7 评论 0原文

嘿,我想问我是否有一个单词列表,比如“老虎、狮子、大象、斑马、马、骆驼、鹿、鳄鱼、兔子、猫” 我怎样才能在c编程中从列表中随机生成5个单词? 例如:

老虎,斑马,猫,鹿,马

鳄鱼,兔子,骆驼,斑马,大象

提前谢谢你:D

编辑

#include <stdio.h> 
#include <string.h>

#define SIZE 10 

int main () 
{ 

char arr2[SIZE][20] = { "tiger", "lion", "elephant", "zebra", "horse", "camel", "deer", "crocodile", "rabbit", "cat" }; 

int x = 0; 
srand(time(NULL));

while (x < SIZE - 5) 
{ 
    arr2 [x][20] = rand (); 
    printf ("%s\n", arr2[x]); 
    x++; 
}

system ("pause"); 
return 0; 
}

hey, i wanna ask if i have a list of words let say 'tiger, lion, elephant, zebra, horse, camel, deer, crocodile, rabbit, cat'
haw can i generate 5 words out of the list randomly in c programming?
for example:

tiger, zebra, cat, deer, horse

or

crocodile, rabbit, camel, zebra, elephant

ect

thank you in advance :D

Edit:

#include <stdio.h> 
#include <string.h>

#define SIZE 10 

int main () 
{ 

char arr2[SIZE][20] = { "tiger", "lion", "elephant", "zebra", "horse", "camel", "deer", "crocodile", "rabbit", "cat" }; 

int x = 0; 
srand(time(NULL));

while (x < SIZE - 5) 
{ 
    arr2 [x][20] = rand (); 
    printf ("%s\n", arr2[x]); 
    x++; 
}

system ("pause"); 
return 0; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难如初 2024-09-21 13:04:15

将单词放入数组中。生成 5 个(或其他)在正确范围 (0..array_size-1) 内的伪随机数。使用这些数字从数组中挑选单词。

Put the words into an array. Generate 5 (or whatever) pseudo-random numbers in the right range (0..array_size-1). Use those numbers to pick words from the array.

桃酥萝莉 2024-09-21 13:04:15

您可以执行以下操作:

  1. 您已经有一个包含元素(动物名称)的数组
  2. 您可以通过索引访问每个元素,例如 k 并且可以像这样访问数组元素 arr2[k]
  3. 现在你每次都需要获得一个分配给 k 的随机数。这可以通过使用标准库的 rand 函数来完成,您可能已经调用过该函数,但方式错误。
  4. 打印出一个值后,您需要跟踪它,因此请使用整数数组check[SIZE] = {0,}并在打印arr2[k]之前,检查是否check[k]==0,然后打印该值。打印后设置arr2[k]=1

完成这些后,请粘贴您的代码。希望您能理解这个问题的逻辑。

You can do the following:

  1. You already have an array which holds the elements(names of the animals)
  2. You can access each element by an index, say k and you can access the array elements like this arr2[k].
  3. Now you need to get a random number assigned to k everytime. This can be done by using the standard library's rand function which you have probably called but in a wrong way
  4. Once you have printed out a value you need to keep track of it, so use a integer array check[SIZE] = {0,}and before printing arr2[k], check if check[k]==0 and then print the value. After printing set arr2[k]=1.

Once you are done with this much please paste your code. Hope you will understand the logic to this problem.

梦途 2024-09-21 13:04:15

出于说明目的,这是 C#,但我确信您可以相当容易地转换为 C:

    static void Main(string[] args)
    {
        string[] words =     
                { "tiger", "lion", "elephant", "zebra", "horse", 
                  "camel", "deer", "crocodile", "rabbit", "cat"  };

        string randomWords = RandomWords.GenerateRandomWordString(5, words); 
    }


public static class RandomWords
{
    private static readonly Random _random = new Random((int)DateTime.Now.Ticks);

    public static string GenerateRandomWordString(int numWords, string[] words)
    {
        int maxlen = words.Length;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numWords; i++)
        {
            // Note: in .NET, Random.Next(0, max) returns 
            // a value in range zero to max - 1
            sb.Append(words[_random.Next(0, maxlen)]);
            sb.Append(" ");
        }

        return sb.ToString().Trim();
    }
}

For illustrative purposes, this is C# but I'm sure you can convert to C, fairly easily:

    static void Main(string[] args)
    {
        string[] words =     
                { "tiger", "lion", "elephant", "zebra", "horse", 
                  "camel", "deer", "crocodile", "rabbit", "cat"  };

        string randomWords = RandomWords.GenerateRandomWordString(5, words); 
    }


public static class RandomWords
{
    private static readonly Random _random = new Random((int)DateTime.Now.Ticks);

    public static string GenerateRandomWordString(int numWords, string[] words)
    {
        int maxlen = words.Length;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numWords; i++)
        {
            // Note: in .NET, Random.Next(0, max) returns 
            // a value in range zero to max - 1
            sb.Append(words[_random.Next(0, maxlen)]);
            sb.Append(" ");
        }

        return sb.ToString().Trim();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文