Getting Random Mover

12/24/2016 19:18 raventh1984#1
Hi Elitepvpers,

I am working on an system but i am stuck atm.

What i am trying to do is to get Random Mover Id's

So it needs to randomly select an ID from MoverProp

currently i have this
Code:
for (int i = 0; i < prj.m_nMoverPropSize; i++)
	{
		MoverProp* pMoverProp = prj.m_pPropMover + i;
		switch(pMoverProp->dwClass)
		{
			case RANK_LOW:
				OUTPUTDEBUGSTRING("\n LowRank Mobs: %d", pMoverProp->dwID);
				break;
		}
	}
first its looping true all the movers then it only outputs the movers who has the class RANK_LOW
so as an example i have this

20
25
112
200
210

so far its good.
What i now need is this

I want to have 5 movers created with an Random id.
so i thought well i do this
Code:
for(int i = 0; i <= 5; i++)
{
        int nRandMover = rand() % pMoverProp->dwID;
}
however it wont work since the output is this
Rand between 0 and 20 will output example 2

But there is no MoverID with number 2. So it will give me an error.

Do i need to put them into an array and then loop true it? or is it something else i am not seeing at this moment?

With kind regards
12/24/2016 20:07 alfredico#2
Code:
vector<MoverProp*> m_monsters;

for (int i = 0; i < prj.m_nMoverPropSize; i++)
{
	MoverProp* pMoverProp = prj.m_pPropMover + i;
	switch(pMoverProp->dwClass)
	{
		case RANK_LOW:
			OUTPUTDEBUGSTRING("\n LowRank Mobs: %d", pMoverProp->dwID);
			m_monsters.push_back(pMoverProp);
			break;
	}
}

for(int i = 0; i <= 5; i++)
{
        int nRandMover = rand() % m_monsters.size();
	m_monsters[nRandMover]->dwID; //Example on how to access the structure.
}
First you need to store the values you want in any container.
On first part add only the needed values to the vector and second part is self explained.
Note that rand() % number returns a random number between 0 and number.
rand() % 20 -> 0, 1, 2, 3, ... 20

If you have more cases in your switch, change for a std::map or std::vector::pair
12/24/2016 20:09 raventh1984#3
Thank you alfredico i will check it out to see if that is what i need. Nicly explained.

Verstuurd vanaf mijn SM-A500FU met Tapatalk
12/25/2016 01:13 Nortix#4
Quote:
Originally Posted by alfredico
Note that rand() % number returns a random number between 0 and number.
rand() % 20 -> 0, 1, 2, 3, ... 20
20 is not an element of coset 20