feat: move random dice to std (#16420)

# Description

As per the suggestion in #16350, this PR moves `random dice` to std.
It's basically a thin wrapper over `random int` already.

# User-Facing Changes (deprecations)

## `random dice` moved to `std`

The `random dice` command has been rewritten in Nushell and moved to the
standard library. The `random dice` built-in is still available with a
deprecation error, but will be removed in 0.108. The new command can be
used as follows:

```nushell
use std/random

random dice
```

It's behavior, parameters, and defaults are the same.


# After Submitting

Update documentation to reflect the change.

Closes #16350
This commit is contained in:
Andrej Kolčin
2025-08-13 18:45:45 +00:00
committed by GitHub
parent 31606a8fe1
commit 4ead4ce4d6
4 changed files with 32 additions and 1 deletions

View File

@ -1,5 +1,5 @@
use nu_engine::command_prelude::*;
use nu_protocol::ListStream;
use nu_protocol::{DeprecationEntry, DeprecationType, ListStream, ReportMode};
use rand::random_range;
use std::num::NonZeroUsize;
@ -38,6 +38,16 @@ impl Command for RandomDice {
vec!["generate", "die", "1-6"]
}
fn deprecation_info(&self) -> Vec<DeprecationEntry> {
vec![DeprecationEntry {
ty: DeprecationType::Command,
report_mode: ReportMode::FirstUse,
since: Some("0.107.0".to_owned()),
expected_removal: Some("0.108.0".to_owned()),
help: Some("Use `random dice` from std/random instead.".to_owned()),
}]
}
fn run(
&self,
engine_state: &EngineState,

View File

@ -65,6 +65,7 @@ pub fn load_standard_library(
include_str!("../std/testing/mod.nu"),
),
("mod.nu", "std/clip", include_str!("../std/clip/mod.nu")),
("mod.nu", "std/random", include_str!("../std/random/mod.nu")),
];
for (filename, std_subdir_name, content) in std_submodules.drain(..) {

View File

@ -16,6 +16,7 @@ export module std/math
export module std/xml
export module std/config
export module std/testing
export module std/random
# Load main dirs command and all subcommands
export use std/dirs main

View File

@ -0,0 +1,19 @@
# for examples
alias "random dice" = dice
# Generate a random dice roll
@search-terms "generate" "die" "1-6"
@example "Roll 1 dice with 6 sides each" { random dice }
@example "Roll 10 dice with 12 sides each" {
random dice --dice 10 --sides 12
}
export def dice [
--dice = 1 # The amount of dice being rolled
--sides = 6 # The amount of sides a die has
]: nothing -> list<int> {
mut out = []
for _ in 1..$dice {
$out ++= [(random int 1..$sides)]
}
$out
}