Generate high precision random number

I’m hoping to effectively make a planar source of varying intensity. With the help of previous posts I am doing this during my GeneratePrimaries(G4Event* anEvent) by generating a random position according to an array of desired intensities. I do this ny having another an array where the value at each index is the accumulative sum of all values in the intensity array up until that point. My plan was to then produce a random value between 0 and the max value of the cumulative array and get the index of the first element greater than this value, however this requires uniform random values between 0 and 7e9.

G4UniformRand seems to only produce values with ~6 decimal places of precision, is there any way to increase this?

Why do you think so? G4UniformRand returns a double in [0,1). If you print out the value (e.g., using G4cout), it’ll only show five or six digits, but that’s because of the default formatting for std::cout. You can increase the precision of printout.

You should be able to multiply the random output by 7e9 to get the range you want.

1 Like

Ah… this is entirely correct and a little embarrassing for me.
Thanks for replying

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.