Page 1 of 1
Random item
Posted: Sat Jan 24, 2015 2:46 pm
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
Re: Random item
Posted: Sat Jan 24, 2015 3:15 pm
by UnAfraid
I'd cache the Rnd.get(100) into a variable.
Re: Random item
Posted: Sat Jan 24, 2015 3:56 pm
by Bencratus
UnAfraid wrote:I'd cache the Rnd.get(100) into a variable.
Did you mean this?:
but the code is correct?
Thanks,
Bencratus
Re: Random item
Posted: Sat Jan 24, 2015 4:48 pm
by UnAfraid
Yes
Re: Random item
Posted: Sat Jan 24, 2015 6:50 pm
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.
Re: Random item
Posted: Sat Jan 24, 2015 7:29 pm
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
Re: Random item
Posted: Sat Jan 24, 2015 7:30 pm
by Zoey76
Depends on what you want, I'd go with static chance (cached at the beginning in variable) as UnAfraid suggested

Re: Random item
Posted: Sat Jan 24, 2015 7:34 pm
by Bencratus
Zoey76 wrote:Depends on what you want, I'd go with static chance (cached at the beginning in variable) as UnAfraid suggested

As you say
Thanks,
Bencratus