D
D
Dmitry Kuznetsov2020-01-30 17:53:00
C++ / C#
Dmitry Kuznetsov, 2020-01-30 17:53:00

Random random with a chance coefficient, how to do?

Hello.

I'm working on one script in the game, but I don't understand how to do it right, because I'm very weak in C ++. Tell me please.

The bottom line is this: there is a script for an action in which one of several items should be issued. But at the same time, each of these items has a different chance of falling out.

I thought of making it a very simple option without a chance coefficient.

override void ChanceItemDrop()
    {
        string itemsRandom[] = {
            "a", "b", "c", "d", "e"
        };

    int min = 0;
    	        int max = 5;
    int intRandom = Math.RandomInt(min, max);   // Получаем случайное число от 0 до 5

                string randItems = itemsRandom[intRandom];  // Получаем предмет из массива "itemsRandom" с помощью рандома "intRandom"

    if(randItems == "a"){
      // Выдаём a
    }else if(randItems == "b"){
      // Выдаём b
    }else if(randItems == "c"){
      // Выдаём c
    }else if(randItems == "d"){
      // Выдаём d
    }else{
      // Выдаём предмет (e)
    }

    // Здесь я выдаю предмет
    }


And it works vprinitspi. How can I make it possible for each item to have a specific drop rate? Or at least tell me where to dig, in what direction.

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman, 2020-01-30
@myjcom

And it works vprinitspi. How can I make it possible for each item to have a specific drop rate? Or at least tell me where to dig, in what direction.

Look:
2 balls are placed in an urn: black and white, what is the probability that the first randomly drawn ball will be white?
Three balls
are placed in an urn: white, black and red, what is the probability that the first randomly drawn ball will be white?
Four balls
are placed in an urn: 2 white, black and red, what is the probability that the first randomly drawn ball will be white?
Keep in mind that the total probability is 1.0
That's how it works, and everything else is nonsense ... .
In other words, you need N pieces of each element in proportion to your drop rate. Then https://en.cppreference.com/w/cpp/algorithm/random...
And then randomly pull from the container, leave them there or delete them, depending on the task.
To use distributions or not is similar. https://en.cppreference.com/w/cpp/numeric/random
I will not undertake to describe more complex options.

T
twobomb, 2020-01-30
@twobomb

Can it be like this

double getRand(){//возвращает рандом от 0 до 1
    return (double)rand()/(double)RAND_MAX;
}

string ChanceItemDrop(){
        string itemsRandom[] = {
            "a", "b", "c", "d", "e"
        };
        
        float itemsChance[] = {
            .30f, .80f, .50f, .30f, .40f //шансы от 0 до 1,  1 -100% , 0 - 0%
        };
        int arrLen = sizeof(itemsChance)/sizeof(*itemsChance);
        double r;
        int arrSize;
        do{
            r = getRand();
            arrSize = 0;
            for(int i=0;i < arrLen;i++)
                arrSize += r <= itemsChance[i]?1:0;
        }while(arrSize == 0);
        int* inxs= new int[arrSize];
        for(int i=0,p = 0;i < arrLen;i++)
            if(r <= itemsChance[i])
                inxs[p++] = i;
        string result = itemsRandom[inxs[(int)floor(getRand()*arrSize)]];
        free(inxs);
        return result;
    }

V
vanyamba-electronics, 2020-01-31
@vanyamba-electronics

float number = ((float) rand()) / MAX_RAND; // число от 0.0 до 1.0
if (number > 0.9)
   cout << "yes" << endl;
else
   cout << "no" << endl;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question