Page MenuHomeFeedback Tracker

Math::RandomFloat01 should exclude 1
Reviewed, NormalPublic

Description

Math::RandomFloat01 is currently sampling from an uniform distribution in the range [0, 1]. The common use case of this method is for handling probabilities:

if (Math::RandomFloat01() < m_fMyProbability)
    // do something

Whether 1 should be included or excluded can be argued based on what happens when m_fMyProbability = 1. In that case the condition should always be true, which is technically only the case when 1 is excluded. Using <= is also not a solution, as we just shift the problem to m_fMyProbability = 0 instead.

Details

Severity
None
Resolution
Open
Reproducibility
Always
Operating System
Windows 11 x64
Category
General
Steps To Reproduce
  1. Read documentation of Math::RandomFloat01

Event Timeline

ookexoo created this task.Mon, Jun 2, 3:45 AM

The common use case of this method is for handling probabilities

Math.RandomFloat01() is not used only for that, and changing the behaviour after release has the ability to break many existing mods and scripts.
Use Math.RandomFloat(0, 1) instead to obtain the behaviour you mention

I thought the point is to pave the way for the future, so backward compatibility should be secondary. Also to consider is that the "default" generators sampling floats from 0 to 1 in modern programming languages are also execluding 1: random.random() in Python, Math.random() in Java, Random.NextDouble() in C#, etc.

Geez changed the task status from New to Reviewed.Tue, Jun 3, 2:56 PM