Plucking strings from an array

Started by
2 comments, last by Paradigm Shifter 10 years, 4 months ago

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).

Editor // Joy-Toilet.com

Anything But Shitty Entertainment!

Advertisement

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).

Editor // Joy-Toilet.com

Anything But Shitty Entertainment!

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.

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).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement