Jump to content

  • Log In with Google      Sign In   
  • Create Account


Plucking strings from an array


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 iGoogleThis   Members   -  Reputation: 205

Like
0Likes
Like

Posted 12 December 2013 - 10:19 AM

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


Edited by iGoogleThis, 12 December 2013 - 01:07 PM.

Chris LeJohn

Build Engineer (RE)
Gnovahex Computing


Sponsor:

#2 iGoogleThis   Members   -  Reputation: 205

Like
0Likes
Like

Posted 12 December 2013 - 01:08 PM

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


Chris LeJohn

Build Engineer (RE)
Gnovahex Computing


#3 Aardvajk   Crossbones+   -  Reputation: 5935

Like
0Likes
Like

Posted 16 December 2013 - 01:20 PM

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.



#4 Paradigm Shifter   Crossbones+   -  Reputation: 5251

Like
0Likes
Like

Posted 16 December 2013 - 03:06 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS