diff --git a/content/cpp/concepts/deque/terms/at/at.md b/content/cpp/concepts/deque/terms/at/at.md new file mode 100644 index 00000000000..859c3394ff9 --- /dev/null +++ b/content/cpp/concepts/deque/terms/at/at.md @@ -0,0 +1,88 @@ +--- +title: at() +description: Access an element at a specific index within a container such as std::deque with bounds checking. +--- + +# at() + +## Description + +The `at()` method in C++ is used to access an element at a specific index within a container, such as a `std::deque`. +It provides bounds checking, ensuring that the requested index is within the valid range of the container. +If the index is invalid, it throws an `std::out_of_range` exception instead of causing undefined behavior. + +--- + +## Syntax + +```cpp +deque_name.at(index); +```` + +**Parameters** + +* `deque_name` — The name of the `std::deque` instance. +* `index` — The zero-based position of the element to access. + +**Returns** +A reference to the element at the specified position. + +**Throws** +`std::out_of_range` — If the index is less than 0 or greater than or equal to the size of the deque. + +--- + +## Example + +```cpp +#include +#include +#include + +int main() { + std::deque d = {10, 20, 30, 40, 50}; + + try { + int value = d.at(2); + std::cout << "Element at index 2: " << value << std::endl; + } catch (const std::out_of_range& e) { + std::cout << "Error: " << e.what() << std::endl; + } + + d.at(0) = 100; + std::cout << "Modified element at index 0: " << d.at(0) << std::endl; + + try { + d.at(10) = 999; + } catch (const std::out_of_range& e) { + std::cout << "Error accessing invalid index: " << e.what() << std::endl; + } + + return 0; +} +``` + +--- + +## Codebyte + +```cpp +#include +#include +#include + +int main() { + std::deque letters = {'A', 'B', 'C', 'D'}; + + char element = letters.at(1); + std::cout << "Element at index 1: " << element << "\n"; + + try { + letters.at(5); + } catch (const std::out_of_range& e) { + std::cout << "Access failed: Out of range exception caught.\n"; + } + + return 0; +} +``` \ No newline at end of file diff --git a/content/python/concepts/random-module/terms/uniform/uniform.md b/content/python/concepts/random-module/terms/uniform/uniform.md index d9c2c415e65..3b1b7646039 100644 --- a/content/python/concepts/random-module/terms/uniform/uniform.md +++ b/content/python/concepts/random-module/terms/uniform/uniform.md @@ -1,53 +1,82 @@ ---- -Title: '.uniform()' -Description: 'Returns a pseudo-random floating-point number between two given numbers.' + + +# Title: 'uniform()' + +# Description: 'The `uniform()` function in NumPy generates random floating-point numbers sampled from a continuous uniform distribution over a specified interval.' Subjects: - 'Computer Science' - 'Data Science' Tags: + - 'NumPy' - 'Random' - - 'Functions' + - 'Uniform Distribution' + - 'Python' CatalogContent: - 'learn-python-3' - 'paths/computer-science' - - 'paths/data-science' --- -The `.uniform()` method takes two numbers as arguments and returns a pseudo-random floating-point number between them. The result is inclusive of the first value, and possibly inclusive of the second value, depending on rounding. +# uniform() + +The `uniform()` function in NumPy generates random floating-point values sampled from a **continuous uniform distribution** over the interval `[low, high)`. +Every value in this range has an equal probability of being selected. + +This function is commonly used in simulations, randomized algorithms, data augmentation, and generating synthetic datasets. + +--- ## Syntax -```pseudo -random.uniform(value1, value2) -``` +```python +numpy.random.uniform(low=0.0, high=1.0, size=None) +```` -Where `value1` and `value2` are numbers bounding the choice of a random floating-point number. +### Parameters -## Example +* **`low`** (float, optional) + The lower bound of the interval. Default is `0.0`. -In the example below, `.uniform()` is used to return a random floating-point number between 10 and 20: +* **`high`** (float, optional) + The upper bound of the interval. Default is `1.0`. -```py -import random +* **`size`** (int or tuple of ints, optional) + The shape of the output. -print(random.uniform(10,20)) -``` + * `None` returns a single float + * An integer returns a 1D array + * A tuple returns a multi-dimensional array -Example output: +### Returns -```shell -13.188312896316244 +* **float or ndarray** + Random value(s) drawn from the specified uniform distribution. + +--- + +## Example + +```python +import numpy as np + +# Generate a single random value between 5 and 10 +value = np.random.uniform(5, 10) +print(value) + +# Generate a 2×3 array of values between 0 and 1 +array = np.random.uniform(0, 1, size=(2, 3)) +print(array) ``` -## Codebyte Example +--- -The following example prints random floating-point numbers between 10 and 15, 100 and 150, -10 and 10, and 0.75 and 0.90. +## Codebyte ```codebyte/python -import random +import numpy as np -print(random.uniform(10,15)) -print(random.uniform(100,150)) -print(random.uniform(-10,10)) -print(random.uniform(0.75,0.90)) +# Generate three random numbers between 2 and 4 +result = np.random.uniform(2, 4, size=3) +print("Uniform random values:", result) ``` + +