Skip to content

Commit f4b783f

Browse files
committed
refactor(python/random): refactor random (wip)
1 parent eb50ee8 commit f4b783f

File tree

6 files changed

+203
-435
lines changed

6 files changed

+203
-435
lines changed

binding/random_binding.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,32 @@ void BindRandom(py::module &m) {
3333
py::class_<RandomGenerator>(m, "RandomGenerator")
3434
.def(py::init<>())
3535
.def(py::init<std::uint32_t>())
36-
.def("generate_int", &RandomGenerator::GenerateInt<std::int64_t>)
37-
.def("generate_real", &RandomGenerator::GenerateReal<double>)
38-
.def("generate_int_list", &RandomGenerator::GenerateIntVector<int>)
39-
.def("generate_real_list", &RandomGenerator::GenerateRealVector<double>)
40-
.def("generate_bool", &RandomGenerator::GenerateBool)
41-
.def("generate_normal", &RandomGenerator::GenerateNormal<double>)
42-
.def("generate_normal_float", &RandomGenerator::GenerateNormal<float>)
36+
.def("rand_int", &RandomGenerator::GenerateInt<std::int64_t>)
37+
.def("rand_float", &RandomGenerator::GenerateReal<double>)
38+
.def("rand_ints", &RandomGenerator::GenerateIntVector<int>)
39+
.def("rand_floats", &RandomGenerator::GenerateRealVector<double>)
40+
.def("rand_bool", &RandomGenerator::GenerateBool)
41+
.def("normal", &RandomGenerator::GenerateNormal<double>)
4342
.def("seed", &RandomGenerator::Seed)
4443
.def("seed_with_time", &RandomGenerator::SeedWithTime)
4544
.def("__repr__", [](const RandomGenerator &self) {
4645
return std::format("<RandomGenerator at {}>", static_cast<const void *>(&self));
4746
});
4847

4948
// Bind utility functions
50-
m.def("shuffle_container", &ShuffleContainer<Container<int>>);
51-
m.def("shuffle_container", &ShuffleContainer<Container<double>>);
52-
m.def("shuffle_container", &ShuffleContainer<Container<std::string>>);
53-
m.def("shuffle_container", &ShuffleContainer<std::vector<int>>);
54-
m.def("shuffle_container", &ShuffleContainer<std::vector<double>>);
55-
m.def("shuffle_container", &ShuffleContainer<std::vector<std::string>>);
56-
m.def("shuffle_container", &ShuffleContainer<std::string>);
49+
m.def("shuffle", &ShuffleContainer<Container<int>>);
50+
m.def("shuffle", &ShuffleContainer<Container<double>>);
51+
m.def("shuffle", &ShuffleContainer<Container<std::string>>);
52+
m.def("shuffle", &ShuffleContainer<std::vector<int>>);
53+
m.def("shuffle", &ShuffleContainer<std::vector<double>>);
54+
m.def("shuffle", &ShuffleContainer<std::vector<std::string>>);
55+
m.def("shuffle", &ShuffleContainer<std::string>);
5756

58-
m.def("sample_from_range", &SampleFromRangeWrapper<Container<int>>);
59-
m.def("sample_from_range", &SampleFromRangeWrapper<Container<double>>);
60-
m.def("sample_from_range", &SampleFromRangeWrapper<Container<std::string>>);
61-
m.def("sample_from_range", &SampleFromRangeWrapper<std::vector<int>>);
62-
m.def("sample_from_range", &SampleFromRangeWrapper<std::vector<double>>);
63-
m.def("sample_from_range", &SampleFromRangeWrapper<std::vector<std::string>>);
64-
m.def("sample_from_range", &SampleFromRangeWrapper<std::string>);
57+
m.def("sample", &SampleFromRangeWrapper<Container<int>>);
58+
m.def("sample", &SampleFromRangeWrapper<Container<double>>);
59+
m.def("sample", &SampleFromRangeWrapper<Container<std::string>>);
60+
m.def("sample", &SampleFromRangeWrapper<std::vector<int>>);
61+
m.def("sample", &SampleFromRangeWrapper<std::vector<double>>);
62+
m.def("sample", &SampleFromRangeWrapper<std::vector<std::string>>);
63+
m.def("sample", &SampleFromRangeWrapper<std::string>);
6564
}

include/random/random_gen.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class RandomGenerator {
8585
* @tparam T Integral type that satisfies std::integral concept
8686
* @param min Minimum value (inclusive)
8787
* @param max Maximum value (inclusive)
88-
* @return Random value of type T in the range [min, max]
88+
* @return A random value of type T in the range [min, max]
8989
*
9090
* Generates a uniformly distributed random integral value within the specified range.
9191
* Both bounds are inclusive. The type T must be an integral type (int, long, etc.).
@@ -107,7 +107,7 @@ class RandomGenerator {
107107
* @tparam T Floating-point type that satisfies std::floating_point concept
108108
* @param min Minimum value (inclusive)
109109
* @param max Maximum value (exclusive)
110-
* @return Random value of type T in the range [min, max)
110+
* @return A random value of type T in the range [min, max)
111111
*
112112
* Generates a uniformly distributed random floating-point value within the specified range.
113113
* The minimum bound is inclusive, the maximum bound is exclusive. The type T must be a
@@ -131,7 +131,7 @@ class RandomGenerator {
131131
* @param min Minimum value for each element (inclusive)
132132
* @param max Maximum value for each element (inclusive)
133133
* @param count Number of random values to generate
134-
* @return Vector containing count random values of type T
134+
* @return A vector of random integral values
135135
*
136136
* Efficiently generates a vector of uniformly distributed random integral values.
137137
* Each value is independently generated within the specified range.
@@ -160,7 +160,7 @@ class RandomGenerator {
160160
* @param min Minimum value for each element (inclusive)
161161
* @param max Maximum value for each element (exclusive)
162162
* @param count Number of random values to generate
163-
* @return Vector containing count random values of type T
163+
* @return A vector of random floating-point values
164164
*
165165
* Efficiently generates a vector of uniformly distributed random floating-point values.
166166
* Each value is independently generated within the specified range.
@@ -186,7 +186,7 @@ class RandomGenerator {
186186
* @brief Generate a random boolean value with specified probability
187187
*
188188
* @param probability Probability of returning true (default: 0.5)
189-
* @return Random boolean value
189+
* @return A random boolean value
190190
*
191191
* Generates a random boolean value using a Bernoulli distribution with the specified probability.
192192
* A probability of 0.5 creates a fair coin flip, while other values bias the outcome accordingly.
@@ -208,7 +208,7 @@ class RandomGenerator {
208208
* @tparam T Floating-point type that satisfies std::floating_point concept
209209
* @param mean Mean (center) of the distribution
210210
* @param stddev Standard deviation of the distribution
211-
* @return Random value from the normal distribution
211+
* @return A random value from the normal distribution
212212
*
213213
* Generates a random value from a normal (Gaussian) distribution with the specified mean and
214214
* standard deviation. This is useful for generating naturally distributed data, noise, or
@@ -293,7 +293,7 @@ void ShuffleContainer(Range &range) {
293293
* @tparam Range Type that satisfies std::ranges::input_range concept
294294
* @param range The source range to sample from
295295
* @param count Number of elements to sample
296-
* @return Vector containing randomly selected elements from the range
296+
* @return A vector of randomly selected elements from the range
297297
*
298298
* Selects a random subset of elements from the input range without replacement.
299299
* If count exceeds the range size, all elements are returned. The relative order of sampled

python/examples/algorithms_example.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
"""Example demonstrating the usage of the algorithms module."""
22

3-
from demo.algorithms import (
4-
count_if,
5-
find_min_max,
6-
pipeline,
7-
sort_inplace,
8-
transform_to_list,
9-
)
3+
from demo.algorithms import *
104
from demo.containers import Container
115

126

137
def main() -> None:
148
"""Run all algorithm examples."""
159
print('=== Algorithms Module Example ===')
1610

17-
# Demonstrate sort_inplace with integers
11+
# Demonstrate sort with integers
1812
numbers = Container(int, [42, 17, 89, 3, 56, 23, 78, 12, 95, 34])
1913
print(f'Original numbers: {numbers}')
2014

21-
sort_inplace(numbers)
15+
sort(numbers)
2216
print(f'Sorted numbers: {numbers}')
2317

2418
# Demonstrate count_if
@@ -40,7 +34,7 @@ def main() -> None:
4034
words = Container(str, ['cherry', 'banana', 'elderberry', 'date', 'apple'])
4135
print(f'Original words: {words}')
4236

43-
sort_inplace(words)
37+
sort(words)
4438
print(f'Sorted words: {words}')
4539

4640
long_words = count_if(words, lambda word: len(word) > 5)

python/src/demo/algorithms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
U = TypeVar('U')
1717

1818

19-
def sort_inplace(data: list[T] | Container[T]) -> None:
19+
def sort(data: list[T] | Container[T]) -> None:
2020
"""Sort a container in-place.
2121
2222
Sorts the elements in the container in ascending order.
@@ -28,9 +28,9 @@ def sort_inplace(data: list[T] | Container[T]) -> None:
2828
2929
Examples
3030
--------
31-
>>> sort_inplace([3, 1, 4, 1, 5, 9, 2, 6, 5, 3])
31+
>>> sort([3, 1, 4, 1, 5, 9, 2, 6, 5, 3])
3232
[1, 1, 2, 3, 3, 4, 5, 5, 6, 9]
33-
>>> sort_inplace(Container(str, ['cherry', 'banana', 'elderberry', 'date', 'apple']))
33+
>>> sort(Container(str, ['cherry', 'banana', 'elderberry', 'date', 'apple']))
3434
['apple', 'banana', 'cherry', 'date', 'elderberry']
3535
"""
3636
match data:
@@ -168,7 +168,7 @@ def composed(data: Any) -> Any:
168168

169169

170170
__all__ = [
171-
'sort_inplace',
171+
'sort',
172172
'count_if',
173173
'transform_to_list',
174174
'find_min_max',

python/src/demo/exceptions.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"""Modern Python wrapper for the exceptions module.
2-
3-
Enhanced error handling with Result types and custom exceptions.
4-
"""
1+
"""Python wrapper for the exceptions module."""
52

63
from enum import Enum
74

0 commit comments

Comments
 (0)