Jump to content

Using gaussian randoms


Recommended Posts

Posted

Hello, 

I am trying to add gaussian randoms to my script. I started using:

int dynamicReturn = (int) ((rand.nextGaussian()*deviation)+mean);

but the issue with that is the Gaussian random will not always stay within the deviation variable. To fix this I did:

int dynamicReturn;
		do {
		dynamicReturn = (int) ((rand.nextGaussian()*deviation)+mean);
		}while(dynamicReturn < (mean-deviation) || dynamicReturn > (mean+deviation));

which works but because that would make it skip generated numbers outside of the range, I'm wondering if that would throw off my gaussian distribution?

If so, what would be a better approach to use that would still lock all the values within my deviation?

Posted
7 minutes ago, Theorems said:

Hello, 

I am trying to add gaussian randoms to my script. I started using:


int dynamicReturn = (int) ((rand.nextGaussian()*deviation)+mean);

but the issue with that is the Gaussian random will not always stay within the deviation variable. To fix this I did:


int dynamicReturn;
		do {
		dynamicReturn = (int) ((rand.nextGaussian()*deviation)+mean);
		}while(dynamicReturn < (mean-deviation) || dynamicReturn > (mean+deviation));

which works but because that would make it skip generated numbers outside of the range, I'm wondering if that would throw off my gaussian distribution?

If so, what would be a better approach to use that would still lock all the values within my deviation?

the way you're doing it is fine, but you'll want to make it within 3 standard deviations to have a gaussian distribution - something like 68% of values are within 1 deviation, 95% in 2, 99.7% in 3 deviations if i remember correctly

Posted
22 minutes ago, Team Cape said:

the way you're doing it is fine, but you'll want to make it within 3 standard deviations to have a gaussian distribution - something like 68% of values are within 1 deviation, 95% in 2, 99.7% in 3 deviations if i remember correctly

Interesting, I have not seen anything done like that before. Doesn't .nextGaussian() give you a Gaussian distribution all by itself without you adding multiple deviations?

 

Posted (edited)
51 minutes ago, Team Cape said:

the way you're doing it is fine, but you'll want to make it within 3 standard deviations to have a gaussian distribution - something like 68% of values are within 1 deviation, 95% in 2, 99.7% in 3 deviations if i remember correctly

That's what nextGaussian() does

@op Just wondering why you are trying to force your values to be within one SD? 

To further explain, if you're forcing all the 'random' values to be within one SD, then that 68 % of the population all have an equal chance of being selected which goes back to a flat distribution (each element has same probability of being selected)

Edited by dreameo
Posted (edited)
19 minutes ago, dreameo said:

That's what nextGaussian() does

@op Just wondering why you are trying to force your values to be within one SD? 

To further explain, if you're forcing all the 'random' values to be within one SD, then that 68 % of the population all have an equal chance of being selected which goes back to a flat distribution (each element has same probability of being selected)

Yeah so it would destroy the Gaussian distribution, that's what I kinda thought might happen. The whole reason I was trying to do it like that in the first place is that I wanted to use it as the return value of my script, which I didn't want to be a zero.

edit: was doing some reading and saw this "There is theoretically no absolute minimum and maximum value that can occur in a normal distribution." So would it really become a flat distribution if you were to just "cut" off the tips of the bell curve of random numbers generated? Doesn't quite make sense to me..

Edited by Theorems
Posted
2 minutes ago, Theorems said:

Yeah so it would destroy the Gaussian distribution, that's what I kinda thought might happen. The whole reason I was trying to do it like that in the first place is that I wanted to use it as the return value of my script, which I didn't want to be a zero.

Well, unless your standard deviation is incredibly high (relative to the mean), it would almost never be 0. 

Posted (edited)
4 minutes ago, dreameo said:

Well, unless your standard deviation is incredibly high (relative to the mean), it would almost never be 0. 

See my edit from my last comment. Also at a mean of 600 and deviation of 300 I was getting some numbers below 0 rarely, but I wouldn't say almost never. Thanks for helping me understand this.

Edited by Theorems
Posted (edited)
10 minutes ago, Theorems said:

See my edit from my last comment. Also at a mean of 600 and deviation of 300 I was getting some numbers below 0 rarely, but I wouldn't say almost never. Thanks for helping me understand this.

Yea so that's fairly high lol. 0 Is within 2 SD which is (97-68 = 27/2 = 13.5) 13.5% probability of occurring. I think this math is wrong actually but you get the idea. 

18 minutes ago, Theorems said:

Yeah so it would destroy the Gaussian distribution, that's what I kinda thought might happen. The whole reason I was trying to do it like that in the first place is that I wanted to use it as the return value of my script, which I didn't want to be a zero.

edit: was doing some reading and saw this "There is theoretically no absolute minimum and maximum value that can occur in a normal distribution." So would it really become a flat distribution if you were to just "cut" of the tips of the bell curve of random numbers generated? Doesn't quite make sense to me..

You're only accepting a range of elements to whom all have the same odds of being selected. So let me demonstrate:

0 mean, 1 std.

-1 and 1 have the same odds of being generated. (1 std vs 1 std)

-1 and 2 don't have the same odds of being generated.  (1 std vs 2 std)

Therefore, any element within 1 std has the same odds of being generated. Since you are confining them to being within 1 std, then it goes to a flat distribution. 

 

lol, someone can double check on my work tho :boge:

Edited by dreameo
  • Like 1
Posted

@dreameo Okay thanks, that makes more sense, although with my example (600 mean, 300 deviation) I think the numbers should still cluster around the mean of 600 even if I only accept numbers within a range which would still make 600 more likely to be generated than 300 or 900. So it wouldn't be a fully normal distribution, but neither would it be flat, if I am understanding correctly.

Posted (edited)
22 minutes ago, Theorems said:

@dreameo Okay thanks, that makes more sense, although with my example (600 mean, 300 deviation) I think the numbers should still cluster around the mean of 600 even if I only accept numbers within a range which would still make 600 more likely to be generated than 300 or 900. So it wouldn't be a fully normal distribution, but neither would it be flat, if I am understanding correctly.

Yea, I tested this. Looks to be true. Numbers closer to the mean are more likely to be generated regardless if they are within the same std. 

 

What I described would be a normal distribution with flat tops lol. 

Edited by dreameo
  • Like 1
Posted (edited)
1 hour ago, dreameo said:

That's what nextGaussian() does

He's only checking within 1 deviation - if he wants to generate a curve that's actually close to gaussian, he'll need to check 3, or else he'll just have the 68%. See the example that he posted above.

1 hour ago, Theorems said:

Yeah so it would destroy the Gaussian distribution, that's what I kinda thought might happen. The whole reason I was trying to do it like that in the first place is that I wanted to use it as the return value of my script, which I didn't want to be a zero.

edit: was doing some reading and saw this "There is theoretically no absolute minimum and maximum value that can occur in a normal distribution." So would it really become a flat distribution if you were to just "cut" off the tips of the bell curve of random numbers generated? Doesn't quite make sense to me..

Computationally, it's not worth dealing with values that have the potential to be incredibly outlandish (i.e. 5000, when you're looking for a value from 4000-4500). the chances of such a value occurring are incredibly small, but regardless, you only have to check one or two boolean statements, so it's worth it to just make the check.

2 hours ago, Theorems said:

Interesting, I have not seen anything done like that before. Doesn't .nextGaussian() give you a Gaussian distribution all by itself without you adding multiple deviations?

It does do that by itself, that's its purpose, but you're only checking 1 deviation in your loop:

}while(dynamicReturn < (mean-deviation) || dynamicReturn > (mean+deviation));

You'll want to make that 3*deviation in both cases.

Edited by Team Cape

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...