# Plucking strings from an array

This topic is 1858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hey GD,  I have some code I'm trying to get working.  the goal is to take one random string from 2 different arrays and print the result.  Based on the tests I've run I'm having trouble getting a string and not just a set of numbers or unknown characters.  Here is a modified (for clarity) example of what I have now.

void NameGen(WINDOW *win)
{
srand(time(NULL));

int lstn = rand() % 5;
int fstn = rand() % 5;

char *surname[5] = { "Carter", "Nagano", "Johnson", "Boustrup", "Smith"};
char *first_name[5] = {"Jeremy", "Chris", "Aya", "Corey", "Eiden"};

wprintw(win, "%s, %s", surname[lstn], first_name[fstn]);

}


EDIT: Fixed, the above example is actually, infact, exactly how one would go about doing this.  By seeding and setting rand() to the same value as the array you essentially leave it up to the program to spit the results back out at you.  The above sample also works with single characters and numbers.  there is plenty of room for optimization, this function is barebones.  I suggest having something offset a recalculation if you plan on calling something like this more than once so that you don't generate different names every time (unless that's what you desire).

##### Share on other sites

hey GD,  I have some code I'm trying to get working.  The goal is to take one random string from 2 different arrays and print the result.  Based on the tests I've run I'm having trouble getting a string and not just a set of numbers or unknown characters for some odd reason.  Here is a modified (for clarity) example of what I have now.

void NameGen(WINDOW *win)
{
srand(time(NULL));

int lstn = rand() % 5;
int fstn = rand() % 5;

char *surname[5] = { "Carter", "Nagano", "Johnson", "Boustrup", "Smith"};
char *first_name[5] = {"Jeremy", "Chris", "Aya", "Corey", "Eiden"};

wprintw(win, "%s, %s", surname[lstn], first_name[fstn]);

}


EDIT: Fixed, the above example is actually, infact, exactly how one would go about doing this.  By seeding and setting rand() to the same value as the array you essentially leave it up to the program to spit the results back out at you.  The above sample also works with single characters and numbers.  there is plenty of room for optimization, this function is barebones.  I suggest having something offset a recalculation if you plan on calling something like this more than once so that you don't generate different names every time (unless that's what you desire).

##### Share on other sites

Its pointless calling srand() more than once in a program, and annoying if you ever want to test with the same set of generated random numbers each time. Call it once at program startup.

C is also a dreadful language to do anything involving text processing unless you have an extremely good reason.

##### Share on other sites

It's very useful calling srand more than once in a program, to reset the random number generator to a known state. Typically you would do that at the beginning of a level or a simulation.

It is useless calling it more than once with an unknown seed though (which is what calling it with time(NULL) does).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634067
• Total Posts
3015323
×