itsadok
itsadok

Reputation: 29342

Generate random small numbers with a target average

I need to write a function that returns on of the numbers (-2,-1,0,1,2) randomly, but I need the average of the output to be a specific number (say, 1.2).

I saw similar questions, but all the answers seem to rely on the target range being wide enough.

Is there a way to do this (without saving state) with this small selection of possible outputs?

UPDATE: I want to use this function for (randomized) testing, as a stub for an expensive function which I don't want to run. The consumer of this function runs it a couple of hundred times and takes an average. I've been using a simple randint function, but the average is always very close to 0, which is not realistic.

Point is, I just need something simple that won't always average to 0. I don't really care what the actual average is. I may have asked the question wrong.

Upvotes: 2

Views: 435

Answers (1)

MvG
MvG

Reputation: 60888

Do you really mean to require that specific value to be the average, or rather the expected value? In other words, if the generated sequence were to contain an extraordinary number of small values in its initial part, should the rest of the sequence atempt to compensate for that in an attempt to get the overall average right? I assume not, I assume you want all your samples to be computed independently (after all, you said you don't want any state), in which case you can only control the expected value.

If you assign a probability pi for each of your possible choices, then the expected value will be the sum of these values, weighted by their probabilities:

EV = − 2p−2p−1 + p1 + 2p2 = 1.2

As additional constraints you have to require that each of these probabilities is non-negative, and that the above four add up to a value less than 1, with the remainder taken by the fifth probability p0.

there are many possible assignments which satisfy these requirements, and any one will do what you asked for. Which of them are reasonable for your application depends on what that application does.

You can use a PRNG which generates variables uniformly distributed in the range [0,1), and then map these to the cases you described by taking the cumulative sums of the probabilities as cut points.

Upvotes: 1

Related Questions