Jump to content

number_ex fix


flatik

Recommended Posts

  • Premium

Hi all!

This is recommended for those who are testing or running on windows. The reason for the error is that they have a 100% chance of dropp.
C++11.

 

Based on the video, you can watch:

 

number_ex old version:

https://metin2.download/video/404cFTu02qN7zGDbO2RA8za82mHM6xzq/.mp4

 

number_ex fixed:

https://metin2.download/video/8084RGh8gld28tLTsoYfx1ITK5W1fWxC/.mp4

 

FIX:

#include <random>

int number_ex(int from, int to, const char* file, int line)
{
    if (from > to)
    {
        int tmp = from;

        sys_err("number(): first argument is bigger than second argument %d -> %d, %s %d", from, to, file, line);

        from = to;
        to = tmp;
    }

    int returnValue = 0;

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> distrib(from, to);

    if ((to - from + 1) != 0)
        returnValue = distrib(gen);
    else
        sys_err("number(): devided by 0");

    return returnValue;
}

 

  • Metin2 Dev 2
  • Good 3
  • Love 10

c++latest, latest libs...

Link to comment
Share on other sites

int number_ex(int from, int to, const char *file, int line)
{
	// We only need (and want) to initialize the mersenne twister generator once
	static std::random_device rd;
	static std::mt19937 mt(rd());
	static std::uniform_int_distribution<int> dist; // not too expensive to create, though

	if (from > to)
	{
		int tmp = from;

		sys_err("number(): first argument is bigger than second argument %d -> %d, %s %d", from, to, file, line);

		from = to;
		to = tmp;
	}

	// Set the range we'd like our distribution to be on, and generate the number
	return dist(mt, std::uniform_int_distribution<int>::param_type(from, to));
}

 

Edited by Etzhel
  • Good 1
  • Love 1
Link to comment
Share on other sites

  • 1 year later...
  • Active Member

For people who might want to use this nowadays, I tested the drops and the upgrade succes percentage and is not so good, very bad drops and a lot of failed upgrades with this uniform random, but even if you use the ymir's function you might have situations where upgrade succeeds in a row or failing in a row

  • Think 1
Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.