views:

81

answers:

3

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; 
}
+3  A: 

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.

Jerry Coffin
i'm blur... i dont get it. :( sorry
falcon
+1  A: 

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();
    }
}
Mitch Wheat
thank you very much!! but seriously, i don't have any idea how to convert it to c... oh... i'm really a stupid =.="
falcon
@falcon: this can be healed by learning, listening and such things, really
Jens Gustedt
@falcon: what have you tried? It won't convert itself...
Mitch Wheat
i have this#include <stdio.h>#include <string.h>#define SIZE 10int 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;}it's really sucks izit?
falcon
hey, i already did it myself!! yes. my code just now was totally garbage and now i already make a new one and it's work!! but i dunno if it's really work corrrectly. for now, as i run it, it can generate five random word. btw, thanks for your help :D
falcon
+1  A: 

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.

Praveen S