From 87735c8c7674b771fc727ee811ffd22fd3157e42 Mon Sep 17 00:00:00 2001 From: Zhiwen Zhao Date: Fri, 12 Jun 2026 22:33:19 -0400 Subject: [PATCH] Fix sphere vertex sampling: compare squared radius to squared bound In-sphere rejection sampling compared x*x+y*y+z*z (a squared magnitude) against the un-squared max_radius, so the accepted region had radius sqrt(max_radius) instead of max_radius. Square the bound. The sampling cube already spans [-max_radius, max_radius] per component, so rejection sampling stays valid. Fixes #109 Co-Authored-By: Claude Fable 5 --- gemc/gparticle/gparticle.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gemc/gparticle/gparticle.cc b/gemc/gparticle/gparticle.cc index c01ab1d3..6f7e7a0e 100644 --- a/gemc/gparticle/gparticle.cc +++ b/gemc/gparticle/gparticle.cc @@ -166,7 +166,7 @@ G4ThreeVector Gparticle::calculateVertex() { z = randomizeNumberFromSigmaWithModel(0, max_radius, gutilities::uniform); radius = x * x + y * y + z * z; } - while (radius > max_radius); + while (radius > max_radius * max_radius); // Offset the sampled point by the nominal vertex. x = x + v.x();