Skip to content

Commit

Permalink
Merge pull request #2690 from notoria/next
Browse files Browse the repository at this point in the history
Add '$random_integer'/3
  • Loading branch information
mthom authored Dec 14, 2024
2 parents 9eef380 + 8cde3f8 commit 5f035ec
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 23 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bytes = "1"
chrono = "0.4.38"
cpu-time = "1.0.0"
crrl = "0.9.0"
dashu = "0.4.2"
dashu = { version = "0.4.2", features = ["rand"] }
derive_more = "0.99.18"
dirs-next = "2.0.0"
divrem = "1.0.0"
Expand Down
4 changes: 4 additions & 0 deletions build/instructions_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ enum SystemClauseType {
GetUnknown,
#[strum_discriminants(strum(props(Arity = "1", Name = "$install_new_block")))]
InstallNewBlock,
#[strum_discriminants(strum(props(Arity = "3", Name = "$random_integer")))]
RandomInteger,
#[strum_discriminants(strum(props(Arity = "0", Name = "$maybe")))]
Maybe,
#[strum_discriminants(strum(props(Arity = "1", Name = "$current_time")))]
Expand Down Expand Up @@ -1805,6 +1807,7 @@ fn generate_instruction_preface() -> TokenStream {
&Instruction::CallGetDoubleQuotes |
&Instruction::CallGetUnknown |
&Instruction::CallInstallNewBlock |
&Instruction::CallRandomInteger |
&Instruction::CallMaybe |
&Instruction::CallCpuNow |
&Instruction::CallDeterministicLengthRundown |
Expand Down Expand Up @@ -2042,6 +2045,7 @@ fn generate_instruction_preface() -> TokenStream {
&Instruction::ExecuteGetDoubleQuotes |
&Instruction::ExecuteGetUnknown |
&Instruction::ExecuteInstallNewBlock |
&Instruction::ExecuteRandomInteger |
&Instruction::ExecuteMaybe |
&Instruction::ExecuteCpuNow |
&Instruction::ExecuteDeterministicLengthRundown |
Expand Down
27 changes: 5 additions & 22 deletions src/lib/random.pl
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
% Succeeds with probability 0.5.
maybe :- '$maybe'.

% The higher the precision, the slower it gets.
random_number_precision(64).

%% random(-R).
%
% Generates a random floating number between 0 (inclusive) and 1 (exclusive).
random(R) :-
var(R),
random_number_precision(N),
rnd(N, R).
N is 2^50,
'$random_integer'(0, N, K),
R is K/N.

%% random_integer(+Lower, +Upper, -R).
%
Expand All @@ -41,25 +39,10 @@
type_error(integer, Lower, random_integer/3)
; \+ integer(Upper) ->
type_error(integer, Upper, random_integer/3)
; Upper > Lower,
random(R0),
R is floor((Upper - Lower) * R0 + Lower)
; Lower < Upper,
'$random_integer'(Lower, Upper, R)
).

rnd(N, R) :-
rnd_(N, 0, R).

rnd_(0, R, R) :- !.
rnd_(N, R0, R) :-
maybe,
!,
N1 is N - 1,
rnd_(N1, R0, R).
rnd_(N, R0, R) :-
N1 is N - 1,
R1 is R0 + 1.0 / 2.0 ^ N,
rnd_(N1, R1, R).

%% set_random(+Seed).
%
% Sets a seed that will be used for subsequent random generations in this library.
Expand Down
8 changes: 8 additions & 0 deletions src/machine/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4061,6 +4061,14 @@ impl Machine {
.install_new_block(self.machine_st.registers[1]);
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
}
&Instruction::CallRandomInteger => {
self.random_integer();
step_or_fail!(self, self.machine_st.p += 1);
}
&Instruction::ExecuteRandomInteger => {
self.random_integer();
step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
}
&Instruction::CallMaybe => {
self.maybe();
step_or_fail!(self, self.machine_st.p += 1);
Expand Down
57 changes: 57 additions & 0 deletions src/machine/system_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4157,6 +4157,63 @@ impl Machine {
}
}

#[inline(always)]
pub(crate) fn random_integer(&mut self) {
let a1 = self.deref_register(1);
let a2 = self.deref_register(2);
let value = match (Number::try_from(a1), Number::try_from(a2)) {
(Ok(Number::Fixnum(lower)), Ok(Number::Fixnum(upper))) => {
let (lower, upper) = (lower.get_num(), upper.get_num());
if lower >= upper {
self.machine_st.fail = true;
return;
}
let value = self.rng.gen_range(lower..upper);
Number::Fixnum(Fixnum::build_with(value))
}
(Ok(Number::Fixnum(lower)), Ok(Number::Integer(upper))) => {
let lower = Integer::from(lower);
if &lower >= &*upper {
self.machine_st.fail = true;
return;
}
let value = self.rng.gen_range(lower..(&*upper).clone());
Number::arena_from(value, &mut self.machine_st.arena)
}
(Ok(Number::Integer(lower)), Ok(Number::Fixnum(upper))) => {
let upper = Integer::from(upper);
if &*lower >= &upper {
self.machine_st.fail = true;
return;
}
let value = self.rng.gen_range((&*lower).clone()..upper);
Number::arena_from(value, &mut self.machine_st.arena)
}
(Ok(Number::Integer(lower)), Ok(Number::Integer(upper))) => {
if &*lower >= &*upper {
self.machine_st.fail = true;
return;
}
let value = self.rng.gen_range((&*lower).clone()..(&*upper).clone());
Number::arena_from(value, &mut self.machine_st.arena)
}
_ => {
self.machine_st.fail = true;
return;
}
};
let a3 = self.deref_register(3);
match value {
Number::Fixnum(n) => {
self.machine_st.unify_fixnum(n, a3);
}
Number::Integer(n) => {
self.machine_st.unify_big_int(n, a3);
}
_ => unreachable!(),
}
}

#[inline(always)]
pub(crate) fn maybe(&mut self) {
self.machine_st.fail = self.rng.gen();
Expand Down

0 comments on commit 5f035ec

Please sign in to comment.