# random number from normal distribution

Page 1 of 1

## 6 Replies - 24015 Views - Last Post: 22 March 2013 - 10:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=111169&amp;s=b8bc1e0ef8392695d851673558f3447e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 usamor

Reputation: 0
• Posts: 96
• Joined: 03-March 09

# random number from normal distribution

Posted 21 June 2009 - 03:34 PM

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.
Is This A Good Question/Topic? 0

## Replies To: random number from normal distribution

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: random number from normal distribution

Posted 21 June 2009 - 03:36 PM

### #3 usamor

Reputation: 0
• Posts: 96
• Joined: 03-March 09

## 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?

```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 tanah.melayu

Reputation: 5
• Posts: 45
• Joined: 21-February 09

## 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?

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

~Happy Coding~

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

### #5 TomDibble

Reputation: 0
• Posts: 2
• Joined: 21-March 13

## 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?

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 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: random number from normal distribution

Posted 22 March 2013 - 03:33 PM

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 TomDibble

Reputation: 0
• Posts: 2
• Joined: 21-March 13

## Re: random number from normal distribution

Posted 22 March 2013 - 10:39 PM

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