Skip to content
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

gen upgrade base implementation #552

Merged
merged 4 commits into from
Sep 26, 2024
Merged
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
4 changes: 2 additions & 2 deletions site/primitives.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,10 +681,10 @@
"description": "Discard the top stack value then call a function"
},
"gen": {
"args": 1,
"args": 2,
"outputs": 2,
"class": "Misc",
"description": "Generate a random number between 0 and 1 from a seed, as well as the next seed"
"description": "Generate random numbers from a shape with a seed."
},
"get": {
"args": 2,
Expand Down
23 changes: 13 additions & 10 deletions src/primitive/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ primitive!(
/// This is usually used to discard values that are no longer needed.
/// For example, [gen] returns both a random number and a seed for the next call.
/// When you have all the random numbers you need, you often want to discard the seed.
/// ex: ⌊×10[◌⍥gen10 0]
/// ex: ⌊×10gen10 0
///
/// [un][pop] can be used to retrieve the [fill] value.
/// ex: ⬚3(+°◌°◌)
Expand Down Expand Up @@ -2639,18 +2639,21 @@ primitive!(
/// If no value is available, then an error is thrown.
/// The error can be caught with [try].
(1, TryRecv, Thread, "tryrecv", Impure),
/// Generate a random number between 0 and 1 from a seed, as well as the next seed
/// Generate an array of random numbers with a seed
///
/// If you don't care about a seed, you can use [random].
///
/// The same seed will always produce the same random number.
/// ex: [◌gen gen gen 0]
/// ex: [◌⍥gen3 0]
/// ex: [◌⍥gen3 1]
/// The first argument is the shape, the second argument is the seed. The returned array will have the given shape where each element is in the range [0, 1).
/// If you don't care about the seed or shape, you can use [random] instead.
/// ex: gen 2_3 0
/// ex: gen 4 42
/// ex: gen 10 42
///
/// Use [multiply] and [floor] to generate a random integer in a range.
/// ex: ⌊*10[◌⍥gen5 0]
(1(2), Gen, Misc, "gen"),
/// ex: ⌊×10 gen 2_3 0
/// ex: ⌊×10 gen 10 42
//
/// For non-determinism, [random] can be used as a seed.
/// ex: gen 3_4 ⚂
(2(2), Gen, Misc, "gen"),
/// Randomly reorder the rows of an array with a seed
///
/// ex: deal0 [1 2 3 4 5]
Expand Down
18 changes: 13 additions & 5 deletions src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ use rand::prelude::*;
use serde::*;

use crate::{
algorithm::{self, *},
algorithm::{self, invert, loops, reduce, table, zip, *},
array::Array,
boxed::Boxed,
check::instrs_signature,
encode,
lex::AsciiToken,
sys::*,
value::*,
FunctionId, Signature, Uiua, UiuaErrorKind, UiuaResult,
FunctionId, Shape, Signature, Uiua, UiuaErrorKind, UiuaResult,
};

/// Categories of primitives
Expand Down Expand Up @@ -830,12 +830,20 @@ impl Primitive {
}
Primitive::Rand => env.push(random()),
Primitive::Gen => {
let seed = env.pop(1)?;
let shape = env
.pop(1)?
.as_nats(env, "Shape should be a single natural or list of naturals")?;
let seed = env.pop(2)?;

let shape = Shape::from(shape);
let mut rng =
SmallRng::seed_from_u64(seed.as_num(env, "Gen expects a number")?.to_bits());
let val: f64 = rng.gen();

let elems: usize = algorithm::validate_size::<f64>(shape.iter().copied(), env)?;
let data = EcoVec::from_iter((0..elems).map(|_| rng.gen::<f64>()));
let next_seed = f64::from_bits(rng.gen::<u64>());
env.push(val);

env.push(Array::new(shape, data));
env.push(next_seed);
}
Primitive::Deal => {
Expand Down
4 changes: 2 additions & 2 deletions tests/macros.ua
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Hi ← Foo!+"a"
a ← 4
b ← (⚂)
c ← comptime(⚂)
d ← gen ⚂
e ← ◌ gen 1
d ← gen []
e ← ◌ gen [] 1

A! ←^ $"_" a ◌
B! ←^ $"_" b ◌
Expand Down