diff --git a/README.md b/README.md index 0f036dc..f72438d 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,9 @@ Add ```using SheepYhangCN.RandomExtension;``` at the head of your C# file # References -```RandomX.Choose(Random inst,value1,value2,value3...)``` => Random selected value
-```RandomX.ChooseInList(Random inst,[value1,value2,value3...])``` => Random selected value
-```RandomX.FiftyFifty(Random inst)``` => true/false
-```RandomX.FiftyFifty(Random inst,value1,value2)``` => 50% chance get value1, 50% chance get value2
-```RandomX.Chance(Random inst,chance in percent)``` => You inserted chance to get true, otherwise get false
-```RandomX.Chance(Random inst,chance in percent,value1,value2,value3...)``` => You inserted chance to get value1, otherwise get a random value in [value2,value3...]
-```RandomX.ChanceInList(Random inst,chance in percent,value1,[value2,value3...])``` => You inserted chance to get value1, otherwise get a random value in [value2,value3...]
-```RandomX.ChancePerItem(Random inst,{[value1,chance],[value2,chance]...})``` => You inserted chances to get values \ No newline at end of file +```.Choose(value1,value2,value3...)``` => Random selected value
+```.FiftyFifty()``` => true/false
+```.FiftyFifty(value1,value2)``` => 50% chance get value1, 50% chance get value2
+```.Chance(chance)``` => You inserted chance to get true, otherwise get false
+```.Chance(chance,value1,value2,value3...)``` => You inserted chance to get value1, otherwise get a random value in [value2,value3...]
+```.ChancePerItem({[value1,chance],[value2,chance]...})``` => You inserted chances to get values \ No newline at end of file diff --git a/README_sChs.md b/README_sChs.md index 725149f..6954221 100644 --- a/README_sChs.md +++ b/README_sChs.md @@ -2,11 +2,9 @@ 把 ```using SheepYhangCN.RandomExtension;``` 添加到你C#文件的开头 # References -```RandomX.Choose(Random实例,value1,value2,value3...)``` => 随机选择value
-```RandomX.ChooseInList(Random实例,[value1,value2,value3...])``` => 随机选择value
-```RandomX.FiftyFifty(Random实例)``` => true/false
-```RandomX.FiftyFifty(Random实例,value1,value2)``` => 50%概率返回value1,50%概率返回value2
-```RandomX.Chance(Random实例,百分制概率)``` => 你输入的概率返回true,否则返回false
-```RandomX.Chance(Random实例,百分制概率,value1,value2,value3...)``` => 你输入的概率返回true,否则从[value2,value3...]中随机返回一个
-```RandomX.ChanceInList(Random实例,百分制概率,value1,[value2,value3...])``` => 你输入的概率返回true,否则从[value2,value3...]中随机返回一个
-```RandomX.ChancePerItem(Random实例,{[value1,概率],[value2,概率]...})``` => 从你输入的概率来返回值 \ No newline at end of file +```.Choose(value1,value2,value3...)``` => 随机选择value
+```.FiftyFifty()``` => true/false
+```.FiftyFifty(value1,value2)``` => 50%概率返回value1,50%概率返回value2
+```.Chance(概率)``` => 你输入的概率返回true,否则返回false
+```.Chance(概率,value1,value2,value3...)``` => 你输入的概率返回value1,否则从[value2,value3...]中随机返回一个
+```.ChancePerItem({[value1,概率],[value2,概率]...})``` => 从你输入的概率来返回值 \ No newline at end of file diff --git a/README_tChs.md b/README_tChs.md index 8a3da55..d1fe493 100644 --- a/README_tChs.md +++ b/README_tChs.md @@ -2,11 +2,9 @@ 把 ```using SheepYhangCN.RandomExtension;``` 添加到你C#檔案的開頭 # References -```RandomX.Choose(Random實例,value1,value2,value3...)``` => 隨機選擇value
-```RandomX.ChooseInList(Random實例,[value1,value2,value3...])``` => 隨機選擇value
-```RandomX.FiftyFifty(Random實例)``` => true/false
-```RandomX.FiftyFifty(Random實例,value1,value2)``` => 50%概率返回value1,50%概率返回value2
-```RandomX.Chance(Random實例,百分制概率)``` => 你輸入的概率返回true,否則返回false
-```RandomX.Chance(Random實例,百分制概率,value1,value2,value3...)``` => 你輸入的概率返回true,否則從[value2,value3...]中隨機返回一個
-```RandomX.ChanceInList(Random實例,百分制概率,value1,[value2,value3...])``` => 你輸入的概率返回true,否則從[value2,value3...]中隨機返回一個
-```RandomX.ChancePerItem(Random實例,{[value1,概率],[value2,概率]...})``` => 從你輸入的概率來返回值 \ No newline at end of file +```.Choose(value1,value2,value3...)``` => 隨機選擇value
+```.FiftyFifty()``` => true/false
+```.FiftyFifty(value1,value2)``` => 50%概率返回value1,50%概率返回value2
+```.Chance(概率)``` => 你輸入的概率返回true,否則返回false
+```.Chance(概率,value1,value2,value3...)``` => 你輸入的概率返回value1,否則從[value2,value3...]中隨機返回一個
+```.ChancePerItem({[value1,概率],[value2,概率]...})``` => 從你輸入的概率來返回值 \ No newline at end of file diff --git a/RandomExtension.cs b/RandomExtension.cs index 9f9cf23..fba7dc8 100644 --- a/RandomExtension.cs +++ b/RandomExtension.cs @@ -1,43 +1,34 @@ //By SheepYhangCN using System; using System.Collections.Generic; -using System.Linq; namespace SheepYhangCN.RandomExtension { public static class RandomX { - public static T Choose(Random random,params T[] values) + public static T Choose(this Random random,params T[] values) { return values[random.Next(values.Length)]; } - public static T ChooseInList(Random random,T[] values) - { - return values[random.Next(values.Length)]; - } - public static bool FiftyFifty(Random random) + public static bool FiftyFifty(this Random random) { return Choose(random,false,true); } - public static T FiftyFifty(Random random,T arg1,T arg2) + public static T FiftyFifty(this Random random,T arg1,T arg2) { return Choose(random,arg1,arg2); } - public static bool Chance(Random random,int chance_percent) - { - return ChooseInList(random,Enumerable.Range(1,100).ToArray()) >= chance_percent; - } - public static T Chance(Random random,int chance_percent,T result,params T[] others) + public static bool Chance(this Random random,double chance) { - return Chance(random,chance_percent) ? result : ChooseInList(random,others); + return chance >= random.NextDouble(); } - public static T ChanceInList(Random random,int chance_percent,T result,T[] others) + public static T Chance(this Random random,double chance,T result,params T[] others) { - return Chance(random,chance_percent) ? result : ChooseInList(random,others); + return Chance(random,chance) ? result : Choose(random,others); } - public static T ChancePerItem(Random random,Dictionary items) + public static T ChancePerItem(this Random random,Dictionary items) { - var r = ChooseInList(random,Enumerable.Range(1,items.Count).ToArray()); + var r = random.Next(1,items.Count); var n = 0; var result = default(T); foreach (var a in items)