U
U
Uncle Bogdan2021-06-23 22:54:32
C++ / C#
Uncle Bogdan, 2021-06-23 22:54:32

How to normally calculate the chance of a character falling out?

Tipo there are some chances of falling out of characters of different rarity. I decided to get a random number from 0-100, and if the number is greater than 94.5, then the player has knocked out a new character. 100 minus that by chance - a chance.
And then comes the verification. The problem is that you can't set the drop chance of a legendary character ( Since it will first check that the number is suitable for a rare character. And in general, the code looks so-so

Code:

Random iterations
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BoxController : PlayerStats
{
    [SerializeField] Box[] Boxes;

    [SerializeField] Text CoinsText;
    [SerializeField] Text PPText;
    public void OpenBox(int BoxId)
    {
        int CoinsAmount = 0;
        int PPAmount = 0;
        for(int i = 0; i < Boxes[BoxId].RandomIterations; i++)
        {
            float Number = Random.Range(0f, 100f);

            if(Number < Boxes[BoxId].CoinsAndPPChance)
            {
                Number = Random.Range(0, 2);

                if(Number == 0)
                {
                    CoinsAmount += Random.Range(Boxes[BoxId].CoinsAmount.x, Boxes[BoxId].CoinsAmount.y);
                }
                else
                {
                    PPAmount += Random.Range(Boxes[BoxId].PPAmount.x, Boxes[BoxId].PPAmount.y);
                }
            }
            else
            {
                Number = 100 - Number;

                print(Number);

                print(Boxes[BoxId].MythicalBrawlerChance);

                if(Number >= Boxes[BoxId].RareBrawlerChance)
                {
                    Debug.LogWarning("Rare BRAWLER");
                }
                else if(Number >= Boxes[BoxId].SuperRareBrawlerChance && Number <= Boxes[BoxId].RareBrawlerChance)
                {
                    Debug.LogWarning("Super RARE BRAWLER");
                }
                else if (Number >= Boxes[BoxId].EpicBrawlerChance && Number <= Boxes[BoxId].SuperRareBrawlerChance)
                {
                    Number = Random.Range(0, 2);
                    if(Number == 0)
                    {
                        Debug.LogWarning("EPIC BRAWLER");
                    }
                    else
                    {
                        Debug.LogWarning("Chromatic");
                    }
                }
                else if (Number >= Boxes[BoxId].MythicalBrawlerChance && Number <= Boxes[BoxId].EpicBrawlerChance)
                {
                    Debug.LogWarning("MYTHICAL BRAWLER");
                }
                else if (Number >= Boxes[BoxId].LegendaryBrawlerChance && Number <= Boxes[BoxId].MythicalBrawlerChance)
                {
                    Debug.LogWarning("LEGA!");
                }
            }
            Money += CoinsAmount;
            PowerPoints += PPAmount;

            CoinsText.text = "Монетки: " + Money;
            PPText.text = "Павер поинты: " + PowerPoints;
        }
    }
}


Random Options
using UnityEngine;
using UnityEngine.UI;

[CreateAssetMenu]
public class Box : ScriptableObject
{
    public int RandomIterations;

    public float CoinsAndPPChance;

    public Vector2Int CoinsAmount;

    public Vector2Int PPAmount;

    public float RareBrawlerChance;

    public float SuperRareBrawlerChance;

    public float EpicBrawlerChance;

    public float MythicalBrawlerChance;

    public float LegendaryBrawlerChance;

    public float ChromaticBrawlerChance;

    public float StarPowerChance;

    public float GadgetChance;

    public float BonusChance;
}


Photo of set values:

3MZMsmy

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2021-06-24
@GavriKos

You need a regular weight random. That's right here in Google and look for.

I
i__egor, 2021-06-24
@i__egor

I use this function for this:
We pass an array of chances (it can be a percentage (their sum is 100) or whatever),

/// <summary>
    /// Функция получает случайный индекс массива, учитывая шанс получить этот самый индекс
    /// (например из: 50, 10, 0, 20) наибольший шанс получит 0 индекс с шансом 50
    /// </summary>
    /// <param name="mas_chance">массив относительных значений шансов получить индекс</param>
    /// <returns></returns>
    public static int GetRandomChanceIndex(int[] mas_chance)
    {
        if (mas_chance.Length == 0)
            return 0;

        int sum = 0;
        for (int i = 0; i < mas_chance.Length; i++)
        {
            sum += mas_chance[i];
        }
    
        int r  = UnityEngine.Random.Range(0, sum);

        sum = 0;
        //int n = 0;
        for (int i = 0; i < mas_chance.Length; i++)
        {
            sum += mas_chance[i];
            if (sum > r)
                return i;
        }

        return 0;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question