- Home
- Skills
- Testing & Debugging
- Make sure "random" is really random
Works with the AI Agents you already use
Make sure "random" is really random
The usual one-line way to shuffle isn't fair — measured, one option came up 28% of the time and another 19%.
Free
Make sure "random" is really random
Example session with this skill installed
Audit my quiz answer shuffling logic and check if the distribution is fair for 4 options.
- Read your context and instructions
- Compiled the make sure random
I have audited the shuffling logic in the quiz component. The existing implementation used a biased sort comparator which skewed the results toward the first position. I have replaced it with a Fisher-Yates shuffle and verified the distribution through simulation.
Randomness Audit & Fix
| Metric | Before Fix (Sort Shuffle) | After Fix (Fisher-Yates) |
|---|---|---|
| Algorithm | array.sort(() => Math.random() - 0.5) | Fisher-Yates (Uniform) |
| Pos 0 Bias | 28.3% (High) | 25.0% (Uniform) |
| Pos 3 Bias | 18.8% (Low) | 25.1% (Uniform) |
| Uniformity | Fails (Skewed) | Passes (Delta < 0.5%) |
/**
* REPLACED: array.sort(() => Math.random() - 0.5)
* WITH: Fisher-Yates implementation for uniform distribution
*/
function shuffle(a) {
const out = [...a];
for (let i = out.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[out[i], out[j]] = [out[j], out[i]];
}
return out;
}
// Distribution Test (200,000 iterations)
// Result: 0: 25.0% | 1: 24.9% | 2: 25.1% | 3: 25.0%
Next steps
- Replace the
shuffleOptionsmethod inQuizEngine.jswith the providedshufflehelper. - Run the included distribution test script in your CI pipeline to prevent regression.
- Update the
LootTable.tsif it uses the same sorting pattern for reward selection.
Connects securely to your tools. The creator never sees your data.
About this skill
The usual one-line way to shuffle an array isn't a shuffle.
sort(() => Math.random() - 0.5) appears in a lot of projects. It looks right and it isn't. Measured over 200,000 runs on four options, here is where the correct answer actually landed:
| Position | Should be 25% | Actually |
|---|---|---|
| First | 25% | 28.3% |
| Second | 25% | 28.2% |
| Third | 25% | 24.7% |
| Fourth | 25% | 18.8% |
Always guessing the first option beat guessing at random. In one real project the same line was in eleven places — shuffling quiz options, picking which questions appeared, choosing rewards.
What it covers
- How to spot a biased shuffle when you read code, and why it looks fine
- The Fisher-Yates fix, written out
- How to actually test a distribution instead of eyeballing a few runs
- The same problem in odds, loot tables and "pick a random subset"
Why it matters
This is a silent bug. Nothing crashes, no error appears, and the output looks random to a human. You find it when a player notices the answer is usually first, or when a reward feels rigged — by which point people have already decided your app cheats.
No terminal, no code, nothing to install. Works in any assistant that takes instructions.
Free, like the other skill on this profile. Read it and judge the approach before paying for anything.
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Testing & Debugging.
Not for CSPRNG/cryptography needs. Assumes Math.random() is sufficient for general application logic. Cannot fix hardware-level entropy issues.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Free to use
No account required to browse
Trust & safety
Security scan
Security scan passed 22 days ago
- Free with an Agensi account
Permissions
No special permissions declared or detected