DarkRewar / BaseTool

A big library of basic tools that you might need in your Unity projects.
MIT License
41 stars 6 forks source link

PonderateRandom #46 #69

Closed DarkRewar closed 5 months ago

DarkRewar commented 5 months ago

PonderateRandom

Many games uses randomizer tweaked to let some elements happens more times than others. It is called "ponderate randomisation". You can create your own by using PonderateRandom class and add weight on each elements ; allowing some to happen more or less than others.

using BaseTool;
using UnityEngine;

var cheatedDice = new PonderateRandom<string> {
    { "One", 1 },
    { "Two", 1 },
    { "Three", 0.5f }, // the three happens twice less than others
    { "Four", 1 },
    { "Five", 1 },
    { "Six", 2 }, // the six happens twice more than others
};

// use UnityEngine.Random
Debug.Log(cheatedDice.Get());

// use System.Random
var random = new System.Random(1);
Debug.Log(cheatedDice.Get(random)); // always be "Two"
Debug.Log(cheatedDice.Get(random)); // always be "One"