clockworklabs / clockworklabs/SpacetimeDB
Feature request: `random.shuffle(array)`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 25.2k
- Forks
- 1.1k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 46
Description
SpacetimeDB's reducers have to use ctx.random for their randomness. It would be great to have a random.shuffle function that would shuffle the given array:
function shuffle<T>(array: T[], random: any/* Random */): T[] {
const maxIndex = array.length - 1;
for (let idx = maxIndex; idx > 0; idx--) {
const swapIdx = random.integerInRange(0, maxIndex);
[array[idx], array[swapIdx]] = [array[swapIdx], array[idx]];
}
}
Note that the Random type is not exported. It would probably be great to export it too.
Context: I'm creating a card game and I have to shuffle cards.
In my project I also have a small utility function to generate Uuid:
function generateUuid(random: any/* Random */): Uuid {
const bytes = new Uint8Array(16);
random.fill(bytes)
return Uuid.fromRandomBytesV4(bytes);
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names the TypeScript ctx.random API and its unexported Random type, but no source file or test. Start by locating the ctx.random entry point and existing random methods. Done means reducers can shuffle arrays through random.shuffle and can access the Random type; cover the behavior with focused tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100