Hi,

can someone tell me how I can generate random number from normal distribution of mean 0 and standard deviation of 0.5 in java (N(0,0.5))

thanks.

# random number from normal distribution

Page 1 of 1## 6 Replies - 21262 Views - Last Post: 22 March 2013 - 10:39 PM

##
**Replies To:** random number from normal distribution

### #2

## Re: random number from normal distribution

Posted 21 June 2009 - 03:36 PM

### #3

## Re: random number from normal distribution

Posted 21 June 2009 - 05:07 PM

can someone tell me what i am doing wrong here:

this is the code that i am using to generate random number from normal distribution of mean 0 and standard deviation of 0.5 which mean random number in the range (-0.5., 0.5).

the problem is that i am getting the random number but the number is also outside of the range. how can i fix this code?

thanks.

this is the code that i am using to generate random number from normal distribution of mean 0 and standard deviation of 0.5 which mean random number in the range (-0.5., 0.5).

the problem is that i am getting the random number but the number is also outside of the range. how can i fix this code?

Double mean=new Double(0.0); Double stDev=new Double(0.0); Double randNorDist=new Double(0.0); mean = 0.0; stDev = 0.5; if (stDev <= 0) { throw new IllegalArgumentException("Gaussian std dev must be > 0"); } Random rand = new Random(); // return randNorDist= stDev * rand.nextGaussian() + mean; // return sss; System.out.println("random number is : " + randNorDist);

thanks.

### #4

## Re: random number from normal distribution

Posted 21 June 2009 - 05:25 PM

usamor, on 21 Jun, 2009 - 04:07 PM, said:

can someone tell me what i am doing wrong here:

this is the code that i am using to generate random number from normal distribution of mean 0 and standard deviation of 0.5 which mean random number in the range (-0.5., 0.5).

the problem is that i am getting the random number but the number is also outside of the range. how can i fix this code?

this is the code that i am using to generate random number from normal distribution of mean 0 and standard deviation of 0.5 which mean random number in the range (-0.5., 0.5).

the problem is that i am getting the random number but the number is also outside of the range. how can i fix this code?

sorry usamor.. i don't have much time to help you fix the code rite now... however, i did a quick search for a solution for your problem. These are 2 links I suggest you to look at:

http://www.javafaq.n...e-code-182.html

http://www.javapract...Action.do?Id=62

I hope, this wilkl help you. Bye!

**~Happy Coding~**

This post has been edited by **tanah.melayu**: 21 June 2009 - 05:26 PM

### #5

## Re: random number from normal distribution

Posted 21 March 2013 - 01:03 PM

usamor, on 21 June 2009 - 05:07 PM, said:

can someone tell me what i am doing wrong here:

this is the code that i am using to generate random number from normal distribution of mean 0 and standard deviation of 0.5 which mean random number in the range (-0.5., 0.5).

the problem is that i am getting the random number but the number is also outside of the range. how can i fix this code?

this is the code that i am using to generate random number from normal distribution of mean 0 and standard deviation of 0.5 which mean random number in the range (-0.5., 0.5).

the problem is that i am getting the random number but the number is also outside of the range. how can i fix this code?

Yes, your expectation of a normal distribution is incorrect. The "standard deviation" means that a little under 70% of the time the number will be +/- the standard deviation.

This means, by definition, that 30% of the time you'll get numbers outside that range.

You need to employ a cutoff mechanism if you want a "truncated" bell curve (where it looks like a normal distribution until it hits a certain point, where frequency drops to zero). The typical approach will loop across the call to nextGaussian until the returned value is in the cutoff range.

Alternatively, perhaps you aren't really looking for a "normal" distribution at all (or, perhaps you want 99% to occur in the "normal" shape, in which case you want 0.5 to be 3x stddev, or stddev = 0.5/3 and cutoff == 0.5). There are other distribution patterns, not the least of which is the "flat" distribution, which is just to be had from Random.nextDouble() which gives a flat distribution between 0.0 and 1.0, so subtract 0.5 to get -0.5 to 0.5.

### #6

## Re: random number from normal distribution

Posted 22 March 2013 - 03:33 PM

Please do not necropost a 4 years old thread

Most of the participants might not still be here

If you have a question open your own thread if you want to show that you know what you are talking about please answer posts being a few days long that will help us

Thank you

Most of the participants might not still be here

If you have a question open your own thread if you want to show that you know what you are talking about please answer posts being a few days long that will help us

Thank you

This post has been edited by **pbl**: 22 March 2013 - 03:35 PM

### #7

## Re: random number from normal distribution

Posted 22 March 2013 - 10:39 PM

pbl, on 22 March 2013 - 03:33 PM, said:

Please do not necropost a 4 years old thread

Most of the participants might not still be here

If you have a question open your own thread if you want to show that you know what you are talking about please answer posts being a few days long that will help us />/>

Thank you

Most of the participants might not still be here

If you have a question open your own thread if you want to show that you know what you are talking about please answer posts being a few days long that will help us />/>

Thank you

For what it's worth, I only posted here because it came up in a Google search. After noticing the age of the last response I didn't expect the poster was still around, but many more people read a thread that's long since moved along when they come across it in a search than participate in it, so I felt it would be worth leaving a correction on it. I'll browse through for more recent posts when I get a chance, but just because a thread is old doesn't mean it has no viewers.

Page 1 of 1