Index: trunk/ppSim/src/ppSimMakeStars.c
===================================================================
--- trunk/ppSim/src/ppSimMakeStars.c	(revision 14667)
+++ trunk/ppSim/src/ppSimMakeStars.c	(revision 14816)
@@ -1,3 +1,6 @@
 # include "ppSim.h"
+
+#define FAINT_FUDGE_FACTOR   1.0        // Fraction of the noise in a (boxcar) PSF for the faint limit
+
 
 bool ppSimMakeStars(psArray *stars, pmFPA *fpa, pmConfig *config, const psRandom *rng) {
@@ -36,27 +39,30 @@
     float readnoise = psMetadataLookupF32(&mdok, recipe, "READNOISE"); // Default read noise
     if (!mdok) {
-	psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Unable to find READNOISE in recipe.");
-	psFree(bounds);
-	return false;
+        psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Unable to find READNOISE in recipe.");
+        psFree(bounds);
+        return false;
     }
 
-    // Peak fluxes: faintest and brightest levels for random stars
-    float faint = 0.1 * 10.0 * sqrtf(PS_SQR(readnoise) + (darkRate + skyRate) * expTime);
-    float bright = powf(10.0, -0.4 * (starsMag - zp)) / sqrt(2.0*M_PI) / seeing * expTime;
+    // Faintest and brightest (integrated) fluxes (actually fluence, since integrated) for random stars
+    float faint = FAINT_FUDGE_FACTOR * sqrtf(PS_SQR(readnoise) + (darkRate + skyRate) * expTime) *
+        seeing; // Faint limit is related to the noise in a (boxcar) PSF
+    float bright = powf(10.0, -0.4 * (starsMag - zp)) * expTime; // Bright limit is specified by user as mag
+    psTrace("ppSim", 6, "Faint limit: %f\n", faint);
+    psTrace("ppSim", 6, "Bright limit: %f\n", bright);
     if (bright < faint) {
-	psLogMsg("ppSim", PS_LOG_INFO,
-		 "Image noise is above brightest random star --- no random stars added.");
-	return true;
+        psLogMsg("ppSim", PS_LOG_INFO,
+                 "Image noise is above brightest random star --- no random stars added.");
+        return true;
     }
 
     // Normalisation, set by the specified stellar density at the specified bright magnitude
     float norm = starsDensity * xSize * ySize * PS_SQR(scale * 180.0 / M_PI) /
-	powf(bright, starsLum);
+        powf(bright, starsLum);
 
     // Total number of stars down to the faint flux end
     long num = expf(logf(norm) + starsLum * logf(faint)) + 0.5;
 
-    psLogMsg("ppSim", PS_LOG_INFO, "Adding %ld stars down to %f mag\n", num,
-	     -2.5 * log10(faint * sqrt(2.0*M_PI) * seeing / expTime) + zp);
+    psLogMsg("ppSim", PS_LOG_INFO, "Adding %ld stars down to %f mag\n",
+             num, -2.5 * log10(faint / expTime) + zp);
 
     long oldSize = stars->n;
@@ -64,16 +70,16 @@
 
     for (long i = 0; i < num; i++) {
-	ppSimStar *star = ppSimStarAlloc ();
+        ppSimStar *star = ppSimStarAlloc ();
 
-	// make fpa center of distribution
-	star->x    = psRandomUniform(rng) * xSize; // x position
-	star->y    = psRandomUniform(rng) * ySize; // y position
+        // make fpa center of distribution
+        star->x    = psRandomUniform(rng) * xSize; // x position
+        star->y    = psRandomUniform(rng) * ySize; // y position
 
-	star->flux = expf((logf(i + 1) - logf(norm)) / starsLum); // Peak flux
-	star->peak = star->flux / (2.0*M_PI*PS_SQR(seeing));
+        star->flux = expf((logf(i + 1) - logf(norm)) / starsLum); // Peak flux
+        star->peak = star->flux / (2.0*M_PI*PS_SQR(seeing));
 
-	stars->data[oldSize + i] = star;
+        stars->data[oldSize + i] = star;
     }
-    stars->n = stars->nalloc;
+    stars->n = oldSize + num;
 
     return true;
