Skip to content

Question: Does the use of Math.random()for pivot selection in std_sortaffect deterministic simulation? #89

@zrcshmily

Description

@zrcshmily

Hello, thank you for this library.

I was examining the std_sortfunction in the codebase and noticed the following line for pivot selection:
// src/particle/b2_particle_system.ts:std_sort
const pivot = array[left + Math.floor(Math.random() * (len - left))]; /* pick random pivot */

My question is: Could the use of Math.random()here have an impact on the determinism​ of the physics simulation? In many physics engines and game/physics applications, deterministic behavior is crucial for features like replay, network synchronization, or consistent debugging across different runs. Since Math.random()is not deterministic, the order of sorting operations might vary between executions, which could potentially lead to different internal states or numerical results in edge cases.

Is this a concern for the guarantees of this physics library? If so, would it be advisable to use a deterministic pseudo-random number generator (with a fixed seed) or a non-random pivot selection strategy (like median-of-three) for applications requiring strict determinism?

I would appreciate your insights on this design choice. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions