Skip to content

Commit

Permalink
improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
SheepYhangCN committed Apr 26, 2024
1 parent 62e814f commit 1af5e61
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 42 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
Add ```using SheepYhangCN.RandomExtension;``` at the head of your C# file

# References
```RandomX.Choose<T>(Random inst,value1,value2,value3...)``` => Random selected value<br>
```RandomX.ChooseInList<T>(Random inst,[value1,value2,value3...])``` => Random selected value<br>
```RandomX.FiftyFifty(Random inst)``` => true/false<br>
```RandomX.FiftyFifty<T>(Random inst,value1,value2)``` => 50% chance get value1, 50% chance get value2<br>
```RandomX.Chance(Random inst,chance in percent)``` => You inserted chance to get true, otherwise get false<br>
```RandomX.Chance<T>(Random inst,chance in percent,value1,value2,value3...)``` => You inserted chance to get value1, otherwise get a random value in [value2,value3...]<br>
```RandomX.ChanceInList<T>(Random inst,chance in percent,value1,[value2,value3...])``` => You inserted chance to get value1, otherwise get a random value in [value2,value3...]<br>
```RandomX.ChancePerItem<T>(Random inst,{[value1,chance],[value2,chance]...})``` => You inserted chances to get values
```<RandomInst>.Choose<T>(value1,value2,value3...)``` => Random selected value<br>
```<RandomInst>.FiftyFifty()``` => true/false<br>
```<RandomInst>.FiftyFifty<T>(value1,value2)``` => 50% chance get value1, 50% chance get value2<br>
```<RandomInst>.Chance(chance)``` => You inserted chance to get true, otherwise get false<br>
```<RandomInst>.Chance<T>(chance,value1,value2,value3...)``` => You inserted chance to get value1, otherwise get a random value in [value2,value3...]<br>
```<RandomInst>.ChancePerItem<T>({[value1,chance],[value2,chance]...})``` => You inserted chances to get values
14 changes: 6 additions & 8 deletions README_sChs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
```using SheepYhangCN.RandomExtension;``` 添加到你C#文件的开头

# References
```RandomX.Choose<T>(Random实例,value1,value2,value3...)``` => 随机选择value<br>
```RandomX.ChooseInList<T>(Random实例,[value1,value2,value3...])``` => 随机选择value<br>
```RandomX.FiftyFifty(Random实例)``` => true/false<br>
```RandomX.FiftyFifty<T>(Random实例,value1,value2)``` => 50%概率返回value1,50%概率返回value2<br>
```RandomX.Chance(Random实例,百分制概率)``` => 你输入的概率返回true,否则返回false<br>
```RandomX.Chance<T>(Random实例,百分制概率,value1,value2,value3...)``` => 你输入的概率返回true,否则从[value2,value3...]中随机返回一个<br>
```RandomX.ChanceInList<T>(Random实例,百分制概率,value1,[value2,value3...])``` => 你输入的概率返回true,否则从[value2,value3...]中随机返回一个<br>
```RandomX.ChancePerItem<T>(Random实例,{[value1,概率],[value2,概率]...})``` => 从你输入的概率来返回值
```<Random实例>.Choose<T>(value1,value2,value3...)``` => 随机选择value<br>
```<Random实例>.FiftyFifty()``` => true/false<br>
```<Random实例>.FiftyFifty<T>(value1,value2)``` => 50%概率返回value1,50%概率返回value2<br>
```<Random实例>.Chance(概率)``` => 你输入的概率返回true,否则返回false<br>
```<Random实例>.Chance<T>(概率,value1,value2,value3...)``` => 你输入的概率返回value1,否则从[value2,value3...]中随机返回一个<br>
```<Random实例>.ChancePerItem<T>({[value1,概率],[value2,概率]...})``` => 从你输入的概率来返回值
14 changes: 6 additions & 8 deletions README_tChs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
```using SheepYhangCN.RandomExtension;``` 添加到你C#檔案的開頭

# References
```RandomX.Choose<T>(Random實例,value1,value2,value3...)``` => 隨機選擇value<br>
```RandomX.ChooseInList<T>(Random實例,[value1,value2,value3...])``` => 隨機選擇value<br>
```RandomX.FiftyFifty(Random實例)``` => true/false<br>
```RandomX.FiftyFifty<T>(Random實例,value1,value2)``` => 50%概率返回value1,50%概率返回value2<br>
```RandomX.Chance(Random實例,百分制概率)``` => 你輸入的概率返回true,否則返回false<br>
```RandomX.Chance<T>(Random實例,百分制概率,value1,value2,value3...)``` => 你輸入的概率返回true,否則從[value2,value3...]中隨機返回一個<br>
```RandomX.ChanceInList<T>(Random實例,百分制概率,value1,[value2,value3...])``` => 你輸入的概率返回true,否則從[value2,value3...]中隨機返回一個<br>
```RandomX.ChancePerItem<T>(Random實例,{[value1,概率],[value2,概率]...})``` => 從你輸入的概率來返回值
```<Random實例>.Choose<T>(value1,value2,value3...)``` => 隨機選擇value<br>
```<Random實例>.FiftyFifty()``` => true/false<br>
```<Random實例>.FiftyFifty<T>(value1,value2)``` => 50%概率返回value1,50%概率返回value2<br>
```<Random實例>.Chance(概率)``` => 你輸入的概率返回true,否則返回false<br>
```<Random實例>.Chance<T>(概率,value1,value2,value3...)``` => 你輸入的概率返回value1,否則從[value2,value3...]中隨機返回一個<br>
```<Random實例>.ChancePerItem<T>({[value1,概率],[value2,概率]...})``` => 從你輸入的概率來返回值
27 changes: 9 additions & 18 deletions RandomExtension.cs
Original file line number Diff line number Diff line change
@@ -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<T>(Random random,params T[] values)
public static T Choose<T>(this Random random,params T[] values)
{
return values[random.Next(values.Length)];
}
public static T ChooseInList<T>(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<T>(Random random,T arg1,T arg2)
public static T FiftyFifty<T>(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<T>(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<T>(Random random,int chance_percent,T result,T[] others)
public static T Chance<T>(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<T>(Random random,Dictionary<T,int> items)
public static T ChancePerItem<T>(this Random random,Dictionary<T,int> 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)
Expand Down

0 comments on commit 1af5e61

Please sign in to comment.