Random item

Support for the latest build of L2J Server, get help here with installations, upgrades, problems.
Do not post bugs reports here, use viewforum.php?f=77 instead.
There is no support for other server builds than the official provided by l2jserver.com
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
User avatar
Bencratus
Posts: 51
Joined: Wed Jan 07, 2015 11:38 pm
Contact:

Random item

Post by Bencratus »

Hello,

can you say it is correct? I want make if people press on item, it is X chance to get reward. But X chance is percent and also you will get only once.

Code: Select all

	if (Rnd.get(100) < 55)
		{
			// example: Adena
		}
		else if (Rnd.get(100) < 30)
		{
			// example: Festival Adena
		}
		else if (Rnd.get(100) < 10)
		{
			// example: Gemstones
		}
		else if (Rnd.get(100) < 5)
		{
                       // example: Draconic Bow
		}
		else
		{
			activeChar.sendMessage("Sorry, you don't get anything...");
		}
Thanks,
Bencratus
User avatar
UnAfraid
L2j Veteran
L2j Veteran
Posts: 4199
Joined: Mon Jul 23, 2007 4:25 pm
Location: Bulgaria
Contact:

Re: Random item

Post by UnAfraid »

I'd cache the Rnd.get(100) into a variable.
Image
User avatar
Bencratus
Posts: 51
Joined: Wed Jan 07, 2015 11:38 pm
Contact:

Re: Random item

Post by Bencratus »

UnAfraid wrote:I'd cache the Rnd.get(100) into a variable.
Did you mean this?:

Code: Select all

int rnd = Rnd.get(100);
but the code is correct?

Thanks,
Bencratus
User avatar
UnAfraid
L2j Veteran
L2j Veteran
Posts: 4199
Joined: Mon Jul 23, 2007 4:25 pm
Location: Bulgaria
Contact:

Re: Random item

Post by UnAfraid »

Yes
Image
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7008
Joined: Tue Aug 11, 2009 3:36 am

Re: Random item

Post by Zoey76 »

Something to keep in mind with this "nested chances" is, taking the first three for example:

Code: Select all

if (Rnd.get(100) < 55)
{
	// example: Adena
}
else if (Rnd.get(100) < 30)
{
	// example: Festival Adena
}
else if (Rnd.get(100) < 10)
{
	// example: Gemstones
}
You have 55% to get Adena, then to get Festival Adena you need to be on the 45% that doesn't get Adena and in the 30% that gets Festival Adena, then the chances are (45*30)/100=13,5% instead of the 30% that you probably wanted, for the last part you get (((45*70)/100)*10)/100=3,15% and so on.
Powered by Eclipse 4.34 ๐ŸŒŒ | Eclipse Temurin 21 โ˜• | MariaDB 11.3.2 ๐Ÿ—ƒ๏ธ | L2J Server 2.6.3.0 - High Five ๐Ÿš€

๐Ÿ”— Join our Discord! ๐ŸŽฎ๐Ÿ’ฌ
User avatar
Bencratus
Posts: 51
Joined: Wed Jan 07, 2015 11:38 pm
Contact:

Re: Random item

Post by Bencratus »

Zoey76 wrote:Something to keep in mind with this "nested chances" is, taking the first three for example:

Code: Select all

if (Rnd.get(100) < 55)
{
	// example: Adena
}
else if (Rnd.get(100) < 30)
{
	// example: Festival Adena
}
else if (Rnd.get(100) < 10)
{
	// example: Gemstones
}
You have 55% to get Adena, then to get Festival Adena you need to be on the 45% that doesn't get Adena and in the 30% that gets Festival Adena, then the chances are (45*30)/100=13,5% instead of the 30% that you probably wanted, for the last part you get (((45*70)/100)*10)/100=3,15% and so on.
Hello, Zoey76,

thanks for the information. But in the math I''m not very good and with chances I suck... So this one maybe will be correct:

Code: Select all

if (Rnd.get(100) < 55)
{
// example: Adena
return false;
}

if (Rnd.get(100) < 30)
{
// example: Festival Adena
return false;
}
Thanks,
Bencratus
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7008
Joined: Tue Aug 11, 2009 3:36 am

Re: Random item

Post by Zoey76 »

Depends on what you want, I'd go with static chance (cached at the beginning in variable) as UnAfraid suggested :P
Powered by Eclipse 4.34 ๐ŸŒŒ | Eclipse Temurin 21 โ˜• | MariaDB 11.3.2 ๐Ÿ—ƒ๏ธ | L2J Server 2.6.3.0 - High Five ๐Ÿš€

๐Ÿ”— Join our Discord! ๐ŸŽฎ๐Ÿ’ฌ
User avatar
Bencratus
Posts: 51
Joined: Wed Jan 07, 2015 11:38 pm
Contact:

Re: Random item

Post by Bencratus »

Zoey76 wrote:Depends on what you want, I'd go with static chance (cached at the beginning in variable) as UnAfraid suggested :P
As you say :)

Thanks,
Bencratus
Post Reply