6 Replies - 15810 Views - Last Post: 22 March 2013 - 10:39 PM Rate Topic: -----

#1 usamor  Icon User is offline

  • D.I.C Head

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

random number from normal distribution

Post icon  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  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: random number from normal distribution

Posted 21 June 2009 - 03:36 PM

http://java.sun.com/...#nextGaussian()
Was This Post Helpful? 0
  • +
  • -

#3 usamor  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 tanah.melayu  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: random number from normal distribution

Posted 21 June 2009 - 05:25 PM

View Postusamor, 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

I hope, this wilkl help you. :) Bye!

~Happy Coding~

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

Was This Post Helpful? 0
  • +
  • -

#5 TomDibble  Icon User is offline

  • New D.I.C Head

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

Re: random number from normal distribution

Posted 21 March 2013 - 01:03 PM

View Postusamor, 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.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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

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

Was This Post Helpful? 0
  • +
  • -

#7 TomDibble  Icon User is offline

  • New D.I.C Head

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

Re: random number from normal distribution

Posted 22 March 2013 - 10:39 PM

View Postpbl, 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


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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1