Skip to content

Commit

Permalink
randomfileadded
Browse files Browse the repository at this point in the history
  • Loading branch information
eshant742 committed Feb 11, 2025
1 parent 9771982 commit de0a45c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const hands = ["rock", "paper", "scissors"] as const;
export function* rockPaperScissors(repetitions?: number): Iterable<"rock" | "paper" | "scissors"> {
let count = 0;
while (repetitions === undefined || count < repetitions) {
yield hands[Math.floor(Math.random() * hands.length)];
count++;
}
}
export async function* rockPaperScissorsAsync(repetitions?: number): AsyncIterable<"rock" | "paper" | "scissors"> {
let count = 0;
while (repetitions === undefined || count < repetitions) {
await Promise.resolve();
yield hands[Math.floor(Math.random() * hands.length)];
count++;
}
}
export const random = {
rockPaperScissors,
rockPaperScissorsAsync,
};
export const Stream = {
rockPaperScissors(repetitions?: number) {
return rockPaperScissors(repetitions);
}
};
export const AsyncStream = {
rockPaperScissors(repetitions?: number) {
return rockPaperScissorsAsync(repetitions);
}
};

0 comments on commit de0a45c

Please sign in to comment.