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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 导入必要的 trait 和模块。
// Import necessary traits and modules.
use ;
use ;
use random;
/// # Biased Random Number Generation
///
/// Generates a non-uniformly distributed random integer in a given range.
///
/// The function accepts any type that implements `RangeBounds<T>`, such as `a..b` or `a..=b`.
/// Note: Ranges with an unbounded end (e.g., `a..` or `..`) are not supported. For ranges with an unbounded start (e.g., `..b`), the start is assumed to be 0.
///
/// The distribution is controlled by a bias parameter, which can make smaller or larger integers more likely.
///
/// # Arguments
///
/// * `range` - The range of random numbers to generate, implementing `RangeBounds<T>`.
/// * `bias` - The bias strength parameter. Must be a positive number.
/// * `bias > 1`: Smaller numbers are more likely. The larger the bias, the more skewed the result is towards the start of the range.
/// * `bias = 1`: Approaches a standard uniform distribution.
/// * `0 < bias < 1`: Larger numbers are more likely (skewed towards the end of the range).
///
/// # Returns
///
/// Returns a random integer within the `range`.
/// If the range is too large or invalid, it returns the start of the range as a fallback.
///
/// # Type Parameters
///
/// * `T` - An integer type that implements `Num`, `Ord`, `ToPrimitive`, `FromPrimitive`, and `Copy`.
///
/// # Examples
///
/// ```
/// // Generate a random number in the range [0, 100), biased towards smaller values.
/// let num = biased::rng(0..100, 3.0);
/// assert!(num >= 0 && num < 100);
///
/// // Use an inclusive range [0, 100]
/// let num_inclusive = biased::rng(0..=100, 3.0);
/// assert!(num_inclusive >= 0 && num_inclusive <= 100);
/// ```
///
/// ---
///
/// # 偏向性随机数生成
///
/// 在指定范围内生成一个非均匀分布的随机整数。
///
/// 函数接受任何实现了 `RangeBounds<T>` 的类型,例如 `a..b` 或 `a..=b`。
/// 注意:不支持末端无限的范围 (例如 `a..` 或 `..`)。对于起始无限的范围 (例如 `..b`),起始值默认为0。
///
/// 分布由一个偏向参数 (bias) 控制,可以使较小或较大的整数更有可能被生成。
///
/// # 参数
///
/// * `range` - 要生成的随机数范围,实现了 `RangeBounds<T>`。
/// * `bias` - 偏向强度参数,必须是正数。
/// * `bias > 1`: 数值越小概率越高,结果越偏向范围的起始值。
/// * `bias = 1`: 接近标准均匀分布。
/// * `0 < bias < 1`: 数值越大概率越高,结果越偏向范围的结束值。
///
/// # 返回值
///
/// 返回一个在 `range` 内的随机整数。
///
/// 如果范围过大或无效,则返回范围的起始值作为后备。
///
/// # 类型参数
///
/// * `T` - 一个整数类型,需要实现 `Num`、`Ord`、`ToPrimitive`、`FromPrimitive` 和 `Copy` trait。
///
/// # 示例
///
/// ```
/// // 生成一个在 [0, 100) 范围内的随机数,偏向于较小的值。
/// let num = biased::rng(0..100, 3.0);
/// assert!(num >= 0 && num < 100);
///
/// // 使用包含性范围 [0, 100]
/// let num_inclusive = biased::rng(0..=100, 3.0);
/// assert!(num_inclusive >= 0 && num_inclusive <= 100);
/// ```