์ค๋ธ์ ํธ๋ค ๋๋คํ๊ฒ ์์ฑํ ๋ ๊ฐ๊ฐ์ ํ๋ฅ ์ ์ค์ผ ํ ๋๊ฐ ์์.
๊ทธ๋ ๋ฌด์์ ์ ํํ ๊ฒ์ธ์ง๋ฅผ ๋ฝ๋ ์ฌํํ ํจ์์.
์๋ ์ฝ๋ ์๋๋ ํ ์คํธ
์ค๊ฐ์ TRandom.range() ๊ฐ ์๋๋ฐ ์ด๊ฑด ์ ๋ํฐ์ Random.range ๋ฑ์ผ๋ก ์นํํด์ ์ฐ๋ฉด ๋จ.ย
(๋ฒ์ ๋ด์ ์์์ ์๋ฅผ ๋ฝ๋ ๋๋คํจ์)
์ฅ์ : ์ ๋ ฅ๊ฐ์ ํ๋ฅ ํฉ์ด 100%๊ฐ ์๋๋ผ๋ ์๊ด์์.
(1, ์ํฌ), (2, ์ฒ ์), (3, ๋๋์ด)
ํ๋ฉด 1:2:3 ํ๋ฅ ๋ก ๋์ด.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | using System.Collections.Generic; using System.Linq; namespace VKLib.Native { public class Frequency<T> { private readonly List<(float, T)> _frequents = new List<(float, T)>(); private float _maxPercent; public Frequency(params (float, T)[] elems) { var threshold = 0f; foreach (var elem in elems) { threshold += elem.Item1; _frequents.Add((threshold, elem.Item2)); } _maxPercent = threshold; } public T GetNextItem() { TDebug.Assert(_frequents.Any()); var randomPercent = TRandom.Range(0f, _maxPercent); for (int i = 0; i < _frequents.Count; i++) { if (randomPercent <= _frequents[i].Item1) { return _frequents[i].Item2; } } return default(T); } } } namespace UnitTestProject { public class Test_Frequency { [Test] public void Test_Freq1() { var freq = new Frequency<char>((1f, 'a'), (10f, 'b'), (100f, 'c')); var res = new List<char>(); for (var i = 0; i < 11100; i++) { res.Add(freq.GetNextItem()); } Assert.IsTrue(res.Count(x => x == 'a') <= 200); Assert.IsTrue(res.Count(x => x == 'b') <= 2000); Assert.IsTrue(res.Count(x => x == 'c') <= 20000); Console.WriteLine($" a : {res.Count(x => x == 'a')}"); Console.WriteLine($" b : {res.Count(x => x == 'b')}"); Console.WriteLine($" c : {res.Count(x => x == 'c')}"); } } } | cs |
์ ๊ธ์์ ์ฝ๋ ๋ํ๋ด๋ ๊ฑฐ ์ข ํ๋๋ค
์ด๋ฐ ๊ฑฐ ์ข๋ค ์ ๊ทธ๋๋ ํ์ํ์ง๋ง ์ข ์ง๊ธฐ ๊ท์ฐฎ์์ ๋งจ ๋ค๋ก ๋ฏธ๋ค๋จ๋๋ฐ ๊ฐ์ฌ ใ ใ