-
Notifications
You must be signed in to change notification settings - Fork 4.3k
randint term added #7701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
randint term added #7701
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0e88a4a
created the required randint.md file
dhruvlad1 c11149a
yarn format
dhruvlad1 1fef8d5
minor fix
mamtawardhani 9f0a7d6
created b2a-base16.md
dhruvlad1 c1a200a
some changes
dhruvlad1 8ba80b0
Apply suggestion from @avdhoottt
avdhoottt bed817c
Apply suggestion from @avdhoottt
avdhoottt b628fce
Apply suggestion from @avdhoottt
avdhoottt 66e432f
Apply suggestion from @avdhoottt
avdhoottt 45ae9b1
Removed unwanted file
avdhoottt 08c952b
Merge branch 'main' into add-randint-term
avdhoottt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
content/numpy/concepts/random-module/terms/randint/randint.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
avdhoottt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```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] | ||
avdhoottt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
avdhoottt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.