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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! Basic random number generation operations
//!
//! This module provides fundamental random number generation functions including
//! uniform distributions, normal distributions, and basic random sampling operations.
//! All operations follow PyTorch's random generation API for compatibility.
use Random;
use RngExt;
use ;
use Tensor;
/// Generate tensor with values drawn from uniform distribution [0, 1)
///
/// ## Mathematical Background
///
/// The uniform distribution U(a,b) has probability density function:
/// ```text
/// f(x) = 1/(b-a) for a ≤ x ≤ b
/// = 0 otherwise
/// ```text
///
/// Properties:
/// - **Mean**: μ = (a+b)/2
/// - **Variance**: σ² = (b-a)²/12
/// - **Support**: [a, b) (a ≤ x < b)
///
/// ## Parameters
/// * `shape` - Shape of the output tensor
/// * `low` - Lower bound (inclusive, default 0.0)
/// * `high` - Upper bound (exclusive, default 1.0)
/// * `generator` - Optional random number generator seed
///
/// ## Returns
/// * Tensor filled with uniformly distributed random values
///
/// ## Example
/// ```rust
/// # use torsh_functional::random_ops::rand;
/// let tensor = rand(&[3, 4], None, None, Some(42))?; // [3, 4] tensor with values in [0, 1)
/// let custom = rand(&[2, 2], Some(-1.0), Some(1.0), None)?; // Values in [-1, 1)
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```text
/// Fill tensor with values drawn from uniform distribution
///
/// ## Mathematical Implementation
///
/// Uses the linear congruential generator method:
/// ```text
/// X_n+1 = (a × X_n + c) mod m
/// U = X_n / m ∈ [0, 1)
/// Y = low + U × (high - low) ∈ [low, high)
/// ```text
///
/// ## Parameters
/// * `shape` - Shape of the tensor
/// * `low` - Lower bound (inclusive)
/// * `high` - Upper bound (exclusive)
/// * `generator` - Optional random number generator seed
///
/// ## Returns
/// * Tensor filled with uniformly distributed values in [low, high)
///
/// ## Errors
/// * Returns error if low >= high
/// Generate tensor with values drawn from normal distribution
///
/// ## Mathematical Background
///
/// The normal (Gaussian) distribution N(μ, σ²) has probability density function:
/// ```text
/// f(x) = (1/(σ√(2π))) × exp(-½((x-μ)/σ)²)
/// ```text
///
/// Properties:
/// - **Mean**: μ
/// - **Variance**: σ²
/// - **Standard deviation**: σ
/// - **Support**: (-∞, ∞)
/// - **68-95-99.7 rule**: ~68% within μ±σ, ~95% within μ±2σ, ~99.7% within μ±3σ
///
/// ## Box-Muller Transformation
///
/// Converts uniform random variables to normal:
/// ```text
/// U₁, U₂ ~ Uniform(0,1)
/// Z₀ = √(-2 ln U₁) × cos(2π U₂)
/// Z₁ = √(-2 ln U₁) × sin(2π U₂)
/// Z₀, Z₁ ~ N(0,1)
/// X = μ + σZ ~ N(μ, σ²)
/// ```text
///
/// ## Parameters
/// * `shape` - Shape of the tensor
/// * `mean` - Mean of the normal distribution
/// * `std` - Standard deviation of the normal distribution
/// * `generator` - Optional random number generator seed
///
/// ## Returns
/// * Tensor filled with normally distributed values N(mean, std²)
/// Generate tensor with values drawn from standard normal distribution
///
/// Equivalent to `normal_(shape, 0.0, 1.0, generator)`.
/// This is a convenience function matching PyTorch's `randn()` API.
///
/// ## Standard Normal Distribution
///
/// The standard normal distribution N(0,1) is the foundation for all normal distributions:
/// - **Mean**: 0
/// - **Variance**: 1
/// - **Standard deviation**: 1
///
/// Any normal distribution can be derived from standard normal:
/// ```text
/// X ~ N(μ, σ²) ⟺ X = μ + σZ where Z ~ N(0,1)
/// ```text
///
/// ## Parameters
/// * `shape` - Shape of the output tensor
/// * `mean` - Mean of the normal distribution (default 0.0)
/// * `std` - Standard deviation of the normal distribution (default 1.0)
/// * `generator` - Optional random number generator seed
///
/// ## Returns
/// * Tensor filled with standard normally distributed values N(0,1)
/// Generate random integers in the range [low, high)
///
/// ## Discrete Uniform Distribution
///
/// For integers in range \[a, b), each value has equal probability:
/// ```text
/// P(X = k) = 1/(b-a) for k ∈ {a, a+1, ..., b-1}
/// = 0 otherwise
/// ```text
///
/// Properties:
/// - **Mean**: μ = (a+b-1)/2
/// - **Variance**: σ² = ((b-a)²-1)/12
/// - **Support**: {a, a+1, ..., b-1}
///
/// ## Parameters
/// * `shape` - Shape of the tensor
/// * `low` - Lower bound (inclusive)
/// * `high` - Upper bound (exclusive)
/// * `generator` - Optional random number generator seed
///
/// ## Returns
/// * Tensor filled with random integers in [low, high)
///
/// ## Errors
/// * Returns error if low >= high
/// Generate random integers in the range [0, high)
///
/// Convenience function equivalent to `randint_(shape, 0, high, generator)`.
///
/// ## Parameters
/// * `shape` - Shape of the tensor
/// * `high` - Upper bound (exclusive)
/// * `generator` - Optional random number generator seed
///
/// ## Returns
/// * Tensor filled with random integers in [0, high)
/// Generate random permutation of integers from 0 to n-1
///
/// ## Mathematical Background
///
/// A random permutation is a bijective mapping π: {0,1,...,n-1} → {0,1,...,n-1}
/// where each of the n! possible permutations has equal probability 1/n!.
///
/// ## Fisher-Yates Shuffle Algorithm
///
/// Modern implementation uses the Fisher-Yates shuffle for O(n) efficiency:
/// ```text
/// for i = n-1 down to 1:
/// j = random(0, i+1)
/// swap(array[i], array[j])
/// ```text
///
/// This algorithm ensures:
/// - **Unbiased**: Each permutation has probability 1/n!
/// - **Efficient**: O(n) time complexity
/// - **In-place**: O(1) additional space
///
/// ## Parameters
/// * `n` - Number of elements to permute (generates 0, 1, ..., n-1)
/// * `generator` - Optional random number generator seed
///
/// ## Returns
/// * 1D tensor containing a random permutation of [0, 1, ..., n-1]
///
/// ## Applications
/// - **Data shuffling**: Randomize dataset order for training
/// - **Sampling**: Create random subsets without replacement
/// - **Bootstrap methods**: Generate random resampling indices
/// - **Monte Carlo**: Random ordering for statistical simulations