Based on how you've phrased the question, this is impossible to do.
Your are essentially asking how to ensure that a single coin flip (i.e. one randomized outcome) is exactly 50% heads and 50% tails, which is impossible.
Even if you were to use two random numbers, where you expect one heads and one tails; this test would fail in 50% of all cases (because you might get two heads or two tails).
Probablility is founded on the law of large numbers. This explicitly states that a small sampling cannot be expected to accurately reflect the expected outcome.
The LLN is important because it guarantees stable long-term results for the averages of some random events. For example, while a casino may lose money in a single spin of the roulette wheel, its earnings will tend towards a predictable percentage over a large number of spins. Any winning streak by a player will eventually be overcome by the parameters of the game. It is important to remember that the law only applies (as the name indicates) when a large number of observations is considered. There is no principle that a small number of observations will coincide with the expected value or that a streak of one value will immediately be "balanced" by the others (see the gambler's fallacy).
When I asked this as a comment; you replied:
@Flater No, I am making N actual draws but with only one random number.
But this doesn't make sense. If you only use one random value, and keep using that same value, then every draw is obviously going to give you the exact same outcome (that same number).
The closest I can interpret your question in a way that is not impossible would be that you were mistakenly referring to a single random seed as a single random number.
A random seed (or seed state, or just seed) is a number (or vector) used to initialize a pseudorandom number generator.
For a seed to be used in a pseudorandom number generator, it does not need to be random. Because of the nature of number generating algorithms, so long as the original seed is ignored, the rest of the values that the algorithm generates will follow probability distribution in a pseudorandom manner.
However, your explicitly mentioned expectations seem to disprove that assumption. You want to do something like:
GetSuccesses( n, P, Random.NextDouble())
and you're also expecting to get a O(1)
operation, which flies in the face of the law of large numbers.
If you're actually talking about having a single random seed; then your expectation are not correct.
- If you make N draws, the operation is still of
O(N)
complexity. Whether you randomize the seed after every draw or not is irrelevant, it's always O(N)
.
GetSuccesses( n, P, Random.NextDouble())
is going to give you one draw, not one seed. Regardless of terminology used, your expectation of the code is not related to using the same seed for several draws.
As the question is currently phrased; what you want is impossible. Repeated comments for clarification by several commenter have not yet yielded a clearer picture.
As a sidenote, I find it very weird that you've answered to every comment except when directly asked if you are talking about a seed instead of a number (twice now).