this post was submitted on 31 Aug 2026
24 points (92.9% liked)
Programming
29100 readers
427 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities !webdev@programming.dev
founded 3 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Be careful of off-by-one errors!
In the range 0-255 there are 256 values, so you need to test 256 % n, which in your example 256 % 3 = 1, so this would give a very slightly uneven distribution.
As to your main point, you're right, and I believe the standard procedure is to just roll the random variable again any time it falls outside the largest integer multiple of the desired output range.
Thank you for the correction! I wrote the post in hurry that's why this mistake.