Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions content/numpy/concepts/random-module/terms/randint/randint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
Title: 'randint()'
Description: 'Generates random integers within a specified range using NumPy.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Numpy'
- 'Python'
- 'Random'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

**`randint()`** is a function from NumPy's `random` module that generates random integers. It can generate a single integer or an array of integers within a specified range, making it useful for simulations, testing, and randomized operations.

## Syntax

```pseudo
numpy.random.randint(low, high=None, size=None, dtype=int)
```

**Parameters:**

- `low` (int): Lowest (inclusive) integer to be drawn.
- `high` (int, optional): One above the highest integer to be drawn. If not provided, integers are drawn from the range `[0, low)`.
- `size` (int or tuple of ints, optional): Output shape. If `None`, a single integer is returned.
- `dtype` (data-type, optional): Desired data type of the output. Default is `int`.

**Return value:**

- `out` (int or ndarray): Random integer(s) from the specified range.
- If `size` is `None`, returns a single integer.
- If `size` is specified, returns a NumPy array of the given shape.

## Example

This example demonstrates generating random integers:

```py
import numpy as np
np.random.seed(15)

# Single random integer from 0 to 9
single_int = np.random.randint(10)
print(single_int)

# Array of 5 random integers from 1 to 5
arr = np.random.randint(1, 6, size=5)
print(arr)
```

The output for this code will be:

```shell
3
[5 2 2 1 2]
```

Here:

- `np.random.seed(15)` ensures reproducible results.
- `np.random.randint(10)` generates a single integer between 0 and 9.
- `np.random.randint(1, 6, size=5)` generates an array of 5 integers between 1 and 5.

## Codebyte Example

This codebyte sample generates a single random integer and a 2×3 array of random integers from specified ranges, ensuring reproducible results using a fixed random seed:

```codebyte/python
import numpy as np
np.random.seed(42)

# Single random integer between 0 and 9
num = np.random.randint(10)
print(f"Random integer: {num}")

# 2x3 array of random integers between 1 and 10
arr = np.random.randint(1, 11, size=(2, 3))
print(f"Random integers array:\n{arr}")
```

- `np.random.seed(42)` ensures the code produces the same results every time.
- `size=(2, 3)` generates a 2D array with 2 rows and 3 columns of random integers.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
Title: '.b2a_base16()'
Description: 'Converts binary data into a bytes object containing a hexadecimal (Base16) ASCII representation.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Encoding'
- 'Functions'
- 'Hexadecimal'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`.b2a_base16()`** [function](https://docs.python.org/3/library/binascii.html#binascii.b2a_base16) converts binary data into a bytes object containing a string of ASCII characters representing the data in Base16 (hexadecimal) format.

## Syntax

```pseudo
binascii.b2a_base16(data, *, newline=False)
```

- `data`: A bytes object containing the binary data to be encoded.
- `*`: This indicates that any arguments following it must be passed using keyword syntax.
- `newline`: When set to True, a newline (`\n`) is appended to the encoded output. Default is False.

## Example

```py
import binascii

# Create binary data to encode
binary_data = b"hello"

# Encode the binary data to Base16 ASCII format
encoded_data = binascii.b2a_base16(binary_data)

print(encoded_data)
```

This produces the following output:

```shell
b'68656C6C6F'
```

> **Note** : Each byte of the input is converted to its two-character hexadecimal representation.

## Codebyte Example

```codebyte/python
import binascii

binary_data = b"Codecademy"
encoded_data = binascii.b2a_base16(binary_data)

print(encoded_data)
```

This produces:

```shell
b'436F6465636164656D79'
```