## How do I generate a range of specific numbers given mean?

generate random numbers with mean and standard deviation matlab

random number normal distribution excel

gaussian random number generator python

normally distributed random numbers - matlab

random number generator excel

randn matlab

random number generator distribution

Given a vector of specified values, for example:

x = c(4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0)

I would like to create a new vector of any length comprised only of values in x sampled randomly, that will result in the combined mean of `3.15`

. I have tried using the `rnorm()`

function but however, I can only generate random numbers that equate to the mean of `3.15`

and not the specified values I wanted. Could anyone point me in the correct direction?

Here's my brute force method:

samp315<-function(n=20, desmean=3.15, distance=0.001) { # create a function with default n=20 and range 3.149-3.151 x<- c(4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0) samp<-0 # reset samp to 0 i<-0 # reset my counter to zero while (!between(mean(samp),desmean-distance,desmean+distance) & i<1000000) { # the following will run continuously until a sample (samp) with a mean that falls within the specified range is found, OR until 1 million attempts have been made samp<-sample(x,n,replace=TRUE) # try to generate a sample of n times from the list of values (x) i=i+1 # add to counter towards 1 million } ifelse(i<1000000,samp,warning("Couldn't find an appropriate sample, please select a lower n, a desired mean closer to 2.5, or a greater distance")) # if the while loop ended because the counter reached a million, exit with an error, otherwise, return the contents of samp. }

Now, every time you do `samp315()`

:

eg<-samp315() mean(eg) [1] 3.15 eg [1] 3.0 3.7 3.0 3.7 3.3 3.7 3.3 3.3 4.0 1.0 1.7 3.0 2.0 4.0 3.7 3.7 2.3 3.3 4.0 3.3

If you want a sample of different length, just place whatever number you wish inside `samp315()`

. The larger the number, however, the longer it will take to find a sample that will get your desired mean.

You can also change your desired mean by setting `desmean`

, and play around with the range by changing `distance`

to whatever the distance (+/-) from your desired mean can be. The default is n=20, with the range from 3.149 to 3.151.

To avoid an infinite loop for highly unlikely combinations of n and range, I set a maximum of 1m samples, after which the function quits with a warning.

**How to Create a Normally Distributed Set of Random Numbers in ,** How do you create a normal distribution of a random number set? You can also change your desired mean by setting desmean, and play around with the range by changing distance to whatever the distance (+/-) from your desired mean can be. The default is n=20, with the range from 3.149 to 3.151.

The problem with your question is that there are an infinite number of ways to sample from

x = c(4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0)

to get a mean of roughly `3.15`

, you just have a specify a probability for each value.

Doing

n = 20 sample(x, n, replace = TRUE)

assumes each value is equally likely and you would get a mean close to `2.5`

. But if you re-weight the probabilities, you can get closer to what you want. One way of doing this might be

p = 1/(x - 3.15)^2 # or try p = 1/abs(x - 3.15) sample(x, n, replace = TRUE, prob = p)

where `p`

weights values closer to `3.15`

higher so these are more likely to be accepted. It isn't perfect (meaning the true expected value is something like `3.12`

and most values are just `2.7`

, `3.0`

and `3.3`

), but then again there isn't a single solution.

**Normal distribution of random numbers (article),** How do you generate random numbers with mean and standard deviation? Random Numbers Within a Specific Range. This example shows how to create an array of random floating-point numbers that are drawn from a uniform distribution in the open interval (50, 100).

As @mickey pointed out, we can weight the probability of each item according to how far it is from the mean. However, that does not quite work, because there are more elements in x lower than than the desired mean, which skews the sampling towards them. We can account for this by adjusting the probabilites relative to how many elements are higher or lower than the desire mean:

x = c(4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0) n = 100000 xbar=3.15 xhi = x[which(x>xbar)] xlo = x[which(x<xbar)] probhi = 1/(xhi-xbar) problo = 1/(xbar-xlo) probhi = probhi * length(problo) / length(probhi) n=1e5 set.seed(1) y = sample(x, size = n, replace = TRUE, prob = c(probhi,problo)) mean(y) # [1] 3.150216

**Generate random number by given certain mean and standard ,** Tip: B3: B16 is the range you randomize numbers in step 2. 4. Now you can generate the real random numbers you need. In cell D3, type this formula =$B$1+(B3 The built-in function range() generates the integer numbers between the given start integer to the stop integer, i.e., It returns a range object. Using for loop, we can iterate over a sequence of numbers produced by the range() function. Let’s understand how to use a range() function of Python 3 with the help of a simple example.

**Random Numbers from Normal Distribution with Specific Mean and ,** This example shows how to create an array of random floating-point numbers that are drawn from a normal distribution having a specified mean and variance. Click on cell D12 to enter that cell reference — the function will search the range selected in the previous step for data that matches this criterion. Although you can input actual data – such as the word East – for this argument, it is usually more convenient to add the data to a cell in the worksheet and then input that cell reference into the dialog box.

**Gaussian Random Number Generator,** Your program could generate a thousand Monkey objects, each with a height values in the range of one standard deviation from the mean, 95% within two different numbers every time the program is run, you must pass a number that if you are looking to generate all the number within a specific rang randomly then you can try. r = randi( [a b],1,d) a = start point. b = end point. d = how many number you want to generate but keep in mind that d should be less than or equal to b-a. share.

**How to Generate a Random Variable With Normal Distribution in Excel,** Note that this generator does not guarantee your numbers to have the exact mean and standard deviation of the distribution from which they are taken. Random Numbers from Normal Distribution with Specific Mean and Variance. This example shows how to create an array of random floating-point numbers that are drawn from a normal distribution having a mean of 500 and variance of 25.

##### Comments

- what is the logic for those specific values?
- Do you need a certain number of values? At low lengths, you can't be sure a set of randomly generated numbers will have a specific mean (they are, after all, random). This will become less of a problem as the number of random numbers grows bigger. Generally speaking, at 500 or so you'll be pretty damn close to 3.5 if you set your random numbers to revolve around that.
- For reference, in most US schools, the values provided are the grade-point weightings of the letter grades (A = 4.0, A- = 3.7, and so on)
- As @iod mentioned, no sampling scheme (even
`rnorm`

) will guarantee that the sample mean is a certain value. Do you want your sample mean to be exactly 3.15 or approximately 3.15? - How close you are depends on your sample size! One solution (maybe not the one you want) is to draw from {3.0, 3.3} with equal probability:
`sample(c(3.0, 3.3), size = 100, replace = TRUE)`

(replace 100 with your desired sample size) - This is amazing thank you so much, I would also appreciate it if you could break down your approach logically!
- I added comments in the answer to explain what each line does. Basically, like I said at the start, it's a brute force method that relies on the fact that if you simulate a sampling long enough, eventually you'll get any possible combination. So I just tell it to sample infinitely (or at least 1 million times), and only stop if the mean of the sample selected falls within a given range.
- I had already tried this method but did not post an answer, because when I test it for large n, it does not give quite the correct mean. same problem with both the
`^2`

and`abs`

versions. I'm not quite sure why it doesn't work - @dww, It's because of the asymmetry around
`3.15`

and insisting on each value having non-zero probability of being selected. If you wanted to have a true mean of`3.15`

, you might have to choose to values for`p`

manually (there are an infinite number of choices). Using`^2`

or`abs`

was meant to act more like a starting point or an approximation. - Notice that
`3.15 = (3.3 + 3)/2`

so that is one solution. You can just repeat an equal number of 3's and 3.3's to get other combinations.`mean(c(rep(3.3,42), rep(3,42))) = 3.15`

But also`(2.3+4)/2 = 3.15`

so you can also add in any number of these pairs.`mean(c(rep(3.3,42), rep(3,42), rep(2.3, 1066), rep(4,1066))) = 3.15`

- But this leaves the problem of not actually getting the desired mean with lower samples. We can combine this solution with my brute force solution to improve the efficiency, while still ensuring the the resulting sample provides the desired mean.