Public Group

# c++ function return question

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

## Recommended Posts

hey, I want to have this return a random type of mob between 1-6 for the number of mobs. Like say NumOfMobs tells me there is 3 mobs, I want TypeOfMob to return a random type of mob for each of the 3.

Basically this function is called when I get to a fight screen and it determines the Number of Mobs and for the Number of mobs picks a random set of mobs from the 1-6 range(TypeOfMob).

So far i can get it to return, but it is returning the same types always. Like if there is 4 mobs and it ends up with 4 of the same type, I want it to be random, not all the same.

#include "stdafx.h"#include <iostream>#include <time.h>using namespace std;int* pType;int NumOfMobs(){	int i=0;	srand(time(NULL));	i=(rand()%3)+1;	return i;}int TypeOfMob(){	int i=0;	srand(time(NULL));	i=(rand()%6)+1;	return i;}int _tmain(int argc, _TCHAR* argv[]){	int num= NumOfMobs();		pType= new int[num];	pType[0]=TypeOfMob();	pType[1]=TypeOfMob();        pType[2]=TypeOfMob();//etc	cout << NumOfMobs() <<"\n" <<pType[0] <<"\n" <<pType[1]<<"\n" <<pType[2]<<"\n" <<pType[3]<<"\n" <<pType[4]<<"\n" <<pType[5];	cin.ignore();	return 0;}

##### Share on other sites
Move srand to your main function. You generally only seed the random number generator once.

##### Share on other sites
cool, that worked. I am just not sure how that will work in the overall large project. Would I just put srand once in the main gameloop and have all the functions work like

#include "stdafx.h"#include <iostream>#include <time.h>using namespace std;int* pType;int NumOfMobs(){	int i=0;	//srand(time(NULL));	i=(rand()%3)+1;	return i;}int TypeOfMob(){	int i=0;	//srand(time(NULL));	i=(rand()%6)+1;	return i;}int _tmain(int argc, _TCHAR* argv[]){	int num= NumOfMobs();	srand(time(NULL));	pType= new int[num];		pType[0]=TypeOfMob();	pType[1]=TypeOfMob();	pType[2]=TypeOfMob();	//etc	cout << NumOfMobs() <<"\n" <<pType[0] <<"\n" <<pType[1]<<"\n" <<pType[2]<<"\n" <<pType[3]<<"\n" <<pType[4]<<"\n" <<pType[5];	cin.ignore();	delete [] pType;	return 0;}

Correct.

##### Share on other sites
ok, thanks a bunch. I haven't used srand much, so I wasn't 100% on how it actually worked. Wasn't sure if it needed to be reseeded each function. Thanks for the quick response!

##### Share on other sites
Of course, you want to make the srand() call before the first call to a function that needs a random number. ;)

Also, try to make functions generic. This '(rand() % X) + 1' form is likely to be common for you, so that is what you should be wrapping up.

Also, don't do your own memory management, and don't write code that generates a random number of elements and then assumes a specific number. :)

And finally, think more about your variable names. Your function names are fine; apply the same principles.

#include "stdafx.h"#include <iostream>#include <time.h>using namespace std;int die_roll(int number_of_sides) {    return (rand() % number_of_sides) + 1;}int _tmain(int argc, _TCHAR* argv[]) {    srand(time(NULL));    int number_of_mobs = die_roll(3);    std::vector<int> mob_types;    // We have to use a loop to initialize the mob types, because we don't know    // how many there are. We should be using a loop anyway, to avoid repeating    // ourselves.    for (int i = 0; i < number_of_mobs; ++i) {        mob_types.push_back(die_roll(6));    }    // Now output works similarly:    cout << number_of_mobs;    for (int i = 0; i < number_of_mobs; ++i) {        cout << '\n' << mob_types;    }    cout << endl;    // Don't pause your programs artificially at the end.}

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
16
5. 5

• 9
• 33
• 13
• 12
• 10
• ### Forum Statistics

• Total Topics
632575
• Total Posts
3007154

×