Skip to content

Commit

Permalink
Add nextBufferElement and nextArrayElement functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekctek committed Jan 14, 2024
1 parent 129a07c commit 5aa3841
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/PseudoRandomX.mo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Module {
nextNat : (min : Nat, max : Nat) -> Nat;
nextCoin : () -> Bool;
nextRatio : (trueCount : Nat, totalCount : Nat) -> Bool;
nextBufferElement : <T>(buffer : Buffer.Buffer<T>) -> T;
nextArrayElement : <T>(array : [T]) -> T;
shuffleBuffer : <T>(buffer : Buffer.Buffer<T>) -> ();
};

Expand Down Expand Up @@ -69,7 +71,7 @@ module Module {
public func nextBufferElement<T>(buffer : Buffer.Buffer<T>) : T {
let bufferSize = buffer.size();
if (bufferSize == 0) {
Debug.trap("Cannot get random element from empty buffer");
Debug.trap("Cannot get random element from an empty buffer");
};
let randomIndex = nextNat(0, bufferSize - 1);
buffer.get(randomIndex);
Expand All @@ -78,7 +80,7 @@ module Module {
public func nextArrayElement<T>(array : [T]) : T {
let arraySize = array.size();
if (arraySize == 0) {
Debug.trap("Cannot get random element from empty buffer");
Debug.trap("Cannot get random element from an empty array");
};
let randomIndex = nextNat(0, arraySize - 1);
array[randomIndex];
Expand Down

0 comments on commit 5aa3841

Please sign in to comment.