随机数算法

   今天给大家讲一下 最简单的随机数,例如杀了一个boss怪会掉落一件装备,这件装备呢可能是长剑,重斧,盾牌,弓箭等等这几种可能,但是每种可能都是有一定概率的,我们一般都不会给概率,假如我们先给他们的概率分别是10%,40%,20%,30%这样非要使得他们的概率和等于1,如果我们需要添加一种的话,那么久需要重新分配了,这样就会影响工作效率,所以一般的我们是给出他们的权重,例如分别为2,3,4,6这种比例,如果需要添加一种的长矛的话我们可以随便给他一个权重10,权重越大表示这种情况占比越大。所以思想就是给们分配一个区间一样例如[0,2],[2,5],[5,9],[9,15],然后我们随机一个数检查这个值在哪个区间,在这个区间的话就代表满足这种情况,思想其实简单,写起来爷比较容易。所以我这里就直接贴出代码,大家把他当成一个工具就行了,不用自己去写。

using System.Collections.Generic;
using UnityEngine;

public class ChanceRoll
{
    private struct Possibility
    {
        public int index;
        public float rarity;
        public bool finished;

        public Possibility(int index, float rarity, bool finished)
        {
            this.index = index;
            this.rarity = rarity;
            this.finished = finished;
        }
    };

    private struct Thing
    {
        float from;
        float to;
        public Possibility pos;

        public Thing(float from, float to, Possibility pos)
        {
            this.from = from;
            this.to = to;
            this.pos = pos;
        }

        //Returns whether or not the given number is within from and to (inclusive)
        public bool isInside(float number)
        {
            return number >= from && number <= to;
        }
    };

    private List list;

    private bool takeAwayPossibilityAfterRoll;

    //if takeAway is true, then the posibility that occurs after a roll is taken out of the next posibilities.
    public ChanceRoll(bool takeAway = false)
    {
        list = new List();
        takeAwayPossibilityAfterRoll = takeAway;
    }


    public void Add(float rarity)
    {
        list.Add(new Possibility(list.Count, rarity, false));
    }


    public int Count()
    {
        return list.Count;
    }


    //Returns the index of the rolled possibility.
    public int Roll()
    {
        float from = 0;
        float to = 0;

        List t = new List();

        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].finished) continue;

            to += list[i].rarity;

            t.Add(new Thing(from, to, list[i]));

            from = to;
        }

        float randomNumber = Random.Range(0, to);
        int winningListIndex = -1;
        for (int i = 0; i < t.Count; i++)
        {
            if (t[i].isInside(randomNumber))
            {
                winningListIndex = t[i].pos.index;
                break;
            }
        }

        if (winningListIndex == -1) return -1;

        if (takeAwayPossibilityAfterRoll)
        {
            list[winningListIndex] = new Possibility(list[winningListIndex].index, list[winningListIndex].rarity,
                true);
        }

        return winningListIndex;
    }
}
用法简单贴一下

 

using UnityEngine;

[ExecutionOrder(1)]
public class Test2 : MonoBehaviour
{


    private ChanceRoll changRoll;
    private void Start()
    {
        Debug.LogError("dddddddddd");
        changRoll=new ChanceRoll();

        changRoll.Add(5);//0
        changRoll.Add(7);//1
        changRoll.Add(8);//2
        changRoll.Add(10);//3


        for (int i = 0; i < 4; i++)
        {
            Debug.LogError(changRoll.Roll());
        }
    }

 
}
通过chanceRoll.Roll方法得到一个值这个值可能是0,1,2,3这个值取决于你chanceRoll数组的长度。

你可能感兴趣的:(面向对象编程)