Urgent.News

What's breaking now, across thousands of outlets.

Tech

True Randomness in Laravel with Lararand

Here is a line of PHP that deals a card from a tarot deck. It is wrong. $card = ord ( random_bytes ( 1 )) % 78 ; Not wrong in the way a linter finds. It returns a number between 0 and 77 every single time. It never throws. Every test you would think to write about it passes: the range is right, the values are distinct, the distribution looks fine if you eyeball a hundred draws. It is wrong in the…

Dealing cards from a tarot deck using PHP can lead to biased results due to the way random_bytes() produces numbers. The issue arises because a byte holds 256 values, while a deck has 78 cards. The largest multiple of 78 that fits into 256 is 234 (78 × 3), leaving 22 values that wrap around and appear four times instead of three. This causes 22 cards to show up 33% more often than the other 56 cards.

Tests that check for the correct range pass, even though the distribution reveals the bias. To fix this, rejection sampling can be used: discard the bytes that land in the uneven tail and draw again. This ensures each card has an equal chance of appearing. This fix should be implemented once in a codebase rather than in every function that turns bytes into numbers.

The same modulo bias problem applies to shuffling arrays. The incorrect method of swapping each position with any other position leads to some permutations being more frequent than others. The correct approach is to swap each position with another position at or below the current one, which avoids the modulo bias.

For applications where the randomness is essential, such as lottery draws or regulated processes, a reliable solution is crucial. Instead of relying on the built-in random_int, a package like lararand can be used. This package allows the use of multiple randomness sources, such as quantum vacuum noise, atmospheric noise, or the system CSPRNG.

The sources are tried left to right, and the first one that provides valid bytes is used. This approach ensures the randomness is not compromised and visible in the configuration file for easy review and adjustment.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Grindr wants to be the everything app for gay men; investors are still deciding whether it can pull it off

George Arison is done letting Wall Street's "Grindr discount" go unchallenged — in a wide-ranging Q&A, the CEO walks us through how AI, a controversial $350-plus EDGE tier, and a bet on healthcare and…

  • Grindr's revenue expected to triple from $195 million in 2022 to $540 million-plus in 2024
  • CEO George Arison aims to transform Grindr into a comprehensive LGBTQ+ platform
  • EDGE subscription with AI-powered matchmaking faces criticism

SaaS: Reactivation with Product Signals

Muchos equipos SaaS mandan emails de reactivación cuando un usuario lleva varios días sin entrar y esperan que eso alcance. A veces funciona un poco. Muchas veces no. El problema no suele ser el asunto del correo, sino la falta de contexto sobre qué dejó de hacer esa persona y por qué deberia volver.

  • Reactivation emails often fail due to lack of context
  • Effective reactivation uses one clear product signal
  • Measure completion of specific step within 72-hour window

More from Monday 31 August →