Skip to content

Commit e5e4ae7

Browse files
committed
added randomGaussian
1 parent 4bc8ab2 commit e5e4ae7

File tree

1 file changed

+171
-38
lines changed

1 file changed

+171
-38
lines changed

core/src/processing/core/PApplet.java

Lines changed: 171 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3616,54 +3616,110 @@ static public final float map(float value,
36163616
Random internalRandom;
36173617

36183618
/**
3619-
* Return a random number in the range [0, howbig).
3620-
* <P>
3621-
* The number returned will range from zero up to
3622-
* (but not including) 'howbig'.
3619+
*
36233620
*/
3624-
public final float random(float howbig) {
3621+
public final float random(float high) {
3622+
// avoid an infinite loop when 0 or NaN are passed in
3623+
if (high == 0 || high != high) {
3624+
return 0;
3625+
}
3626+
3627+
if (internalRandom == null) {
3628+
internalRandom = new Random();
3629+
}
3630+
36253631
// for some reason (rounding error?) Math.random() * 3
36263632
// can sometimes return '3' (once in ~30 million tries)
36273633
// so a check was added to avoid the inclusion of 'howbig'
3634+
float value = 0;
3635+
do {
3636+
value = internalRandom.nextFloat() * high;
3637+
} while (value == high);
3638+
return value;
3639+
}
36283640

3629-
// avoid an infinite loop
3630-
if (howbig == 0) return 0;
3641+
/**
3642+
* ( begin auto-generated from randomGaussian.xml )
3643+
*
3644+
* Returns a float from a random series of numbers having a mean of 0
3645+
* and standard deviation of 1. Each time the <b>randomGaussian()</b>
3646+
* function is called, it returns a number fitting a Gaussian, or
3647+
* normal, distribution. There is theoretically no minimum or maximum
3648+
* value that <b>randomGaussian()</b> might return. Rather, there is
3649+
* just a very low probability that values far from the mean will be
3650+
* returned; and a higher probability that numbers near the mean will
3651+
* be returned.
3652+
*
3653+
* ( end auto-generated )
3654+
* @webref math:random
3655+
* @see PApplet#random(float,float)
3656+
* @see PApplet#noise(float, float, float)
3657+
*/
3658+
public final float randomGaussian() {
3659+
if (internalRandom == null) {
3660+
internalRandom = new Random();
3661+
}
3662+
return (float) internalRandom.nextGaussian();
3663+
}
36313664

3632-
// internal random number object
3633-
if (internalRandom == null) internalRandom = new Random();
36343665

3666+
/**
3667+
* ( begin auto-generated from random.xml )
3668+
*
3669+
* Generates random numbers. Each time the <b>random()</b> function is
3670+
* called, it returns an unexpected value within the specified range. If
3671+
* one parameter is passed to the function it will return a <b>float</b>
3672+
* between zero and the value of the <b>high</b> parameter. The function
3673+
* call <b>random(5)</b> returns values between 0 and 5 (starting at zero,
3674+
* up to but not including 5). If two parameters are passed, it will return
3675+
* a <b>float</b> with a value between the the parameters. The function
3676+
* call <b>random(-5, 10.2)</b> returns values starting at -5 up to (but
3677+
* not including) 10.2. To convert a floating-point random number to an
3678+
* integer, use the <b>int()</b> function.
3679+
*
3680+
* ( end auto-generated )
3681+
* @webref math:random
3682+
* @param low lower limit
3683+
* @param high upper limit
3684+
* @see PApplet#randomSeed(long)
3685+
* @see PApplet#noise(float, float, float)
3686+
*/
3687+
public final float random(float low, float high) {
3688+
if (low >= high) return low;
3689+
float diff = high - low;
36353690
float value = 0;
3691+
// because of rounding error, can't just add low, otherwise it may hit high
3692+
// https://github.com/processing/processing/issues/4551
36363693
do {
3637-
//value = (float)Math.random() * howbig;
3638-
value = internalRandom.nextFloat() * howbig;
3639-
} while (value == howbig);
3694+
value = random(diff) + low;
3695+
} while (value == high);
36403696
return value;
36413697
}
36423698

36433699

36443700
/**
3645-
* Return a random number in the range [howsmall, howbig).
3646-
* <P>
3647-
* The number returned will range from 'howsmall' up to
3648-
* (but not including 'howbig'.
3649-
* <P>
3650-
* If howsmall is >= howbig, howsmall will be returned,
3651-
* meaning that random(5, 5) will return 5 (useful)
3652-
* and random(7, 4) will return 7 (not useful.. better idea?)
3701+
* ( begin auto-generated from randomSeed.xml )
3702+
*
3703+
* Sets the seed value for <b>random()</b>. By default, <b>random()</b>
3704+
* produces different results each time the program is run. Set the
3705+
* <b>value</b> parameter to a constant to return the same pseudo-random
3706+
* numbers each time the software is run.
3707+
*
3708+
* ( end auto-generated )
3709+
* @webref math:random
3710+
* @param seed seed value
3711+
* @see PApplet#random(float,float)
3712+
* @see PApplet#noise(float, float, float)
3713+
* @see PApplet#noiseSeed(long)
36533714
*/
3654-
public final float random(float howsmall, float howbig) {
3655-
if (howsmall >= howbig) return howsmall;
3656-
float diff = howbig - howsmall;
3657-
return random(diff) + howsmall;
3715+
public final void randomSeed(long seed) {
3716+
if (internalRandom == null) {
3717+
internalRandom = new Random();
3718+
}
3719+
internalRandom.setSeed(seed);
36583720
}
36593721

36603722

3661-
public final void randomSeed(long what) {
3662-
// internal random number object
3663-
if (internalRandom == null) internalRandom = new Random();
3664-
internalRandom.setSeed(what);
3665-
}
3666-
36673723

36683724

36693725
//////////////////////////////////////////////////////////////
@@ -3700,22 +3756,56 @@ public final void randomSeed(long what) {
37003756

37013757

37023758
/**
3703-
* Computes the Perlin noise function value at point x.
37043759
*/
37053760
public float noise(float x) {
37063761
// is this legit? it's a dumb way to do it (but repair it later)
37073762
return noise(x, 0f, 0f);
37083763
}
37093764

37103765
/**
3711-
* Computes the Perlin noise function value at the point x, y.
37123766
*/
37133767
public float noise(float x, float y) {
37143768
return noise(x, y, 0f);
37153769
}
37163770

37173771
/**
3718-
* Computes the Perlin noise function value at x, y, z.
3772+
* ( begin auto-generated from noise.xml )
3773+
*
3774+
* Returns the Perlin noise value at specified coordinates. Perlin noise is
3775+
* a random sequence generator producing a more natural ordered, harmonic
3776+
* succession of numbers compared to the standard <b>random()</b> function.
3777+
* It was invented by Ken Perlin in the 1980s and been used since in
3778+
* graphical applications to produce procedural textures, natural motion,
3779+
* shapes, terrains etc.<br /><br /> The main difference to the
3780+
* <b>random()</b> function is that Perlin noise is defined in an infinite
3781+
* n-dimensional space where each pair of coordinates corresponds to a
3782+
* fixed semi-random value (fixed only for the lifespan of the program).
3783+
* The resulting value will always be between 0.0 and 1.0. Processing can
3784+
* compute 1D, 2D and 3D noise, depending on the number of coordinates
3785+
* given. The noise value can be animated by moving through the noise space
3786+
* as demonstrated in the example above. The 2nd and 3rd dimension can also
3787+
* be interpreted as time.<br /><br />The actual noise is structured
3788+
* similar to an audio signal, in respect to the function's use of
3789+
* frequencies. Similar to the concept of harmonics in physics, perlin
3790+
* noise is computed over several octaves which are added together for the
3791+
* final result. <br /><br />Another way to adjust the character of the
3792+
* resulting sequence is the scale of the input coordinates. As the
3793+
* function works within an infinite space the value of the coordinates
3794+
* doesn't matter as such, only the distance between successive coordinates
3795+
* does (eg. when using <b>noise()</b> within a loop). As a general rule
3796+
* the smaller the difference between coordinates, the smoother the
3797+
* resulting noise sequence will be. Steps of 0.005-0.03 work best for most
3798+
* applications, but this will differ depending on use.
3799+
*
3800+
* ( end auto-generated )
3801+
*
3802+
* @webref math:random
3803+
* @param x x-coordinate in noise space
3804+
* @param y y-coordinate in noise space
3805+
* @param z z-coordinate in noise space
3806+
* @see PApplet#noiseSeed(long)
3807+
* @see PApplet#noiseDetail(int, float)
3808+
* @see PApplet#random(float,float)
37193809
*/
37203810
public float noise(float x, float y, float z) {
37213811
if (perlin == null) {
@@ -3739,9 +3829,9 @@ public float noise(float x, float y, float z) {
37393829
if (z<0) z=-z;
37403830

37413831
int xi=(int)x, yi=(int)y, zi=(int)z;
3742-
float xf = (float)(x-xi);
3743-
float yf = (float)(y-yi);
3744-
float zf = (float)(z-zi);
3832+
float xf = x - xi;
3833+
float yf = y - yi;
3834+
float zf = z - zi;
37453835
float rxf, ryf;
37463836

37473837
float r=0;
@@ -3796,18 +3886,61 @@ private float noise_fsc(float i) {
37963886
// for different levels of detail. lower values will produce
37973887
// smoother results as higher octaves are surpressed
37983888

3889+
/**
3890+
* ( begin auto-generated from noiseDetail.xml )
3891+
*
3892+
* Adjusts the character and level of detail produced by the Perlin noise
3893+
* function. Similar to harmonics in physics, noise is computed over
3894+
* several octaves. Lower octaves contribute more to the output signal and
3895+
* as such define the overal intensity of the noise, whereas higher octaves
3896+
* create finer grained details in the noise sequence. By default, noise is
3897+
* computed over 4 octaves with each octave contributing exactly half than
3898+
* its predecessor, starting at 50% strength for the 1st octave. This
3899+
* falloff amount can be changed by adding an additional function
3900+
* parameter. Eg. a falloff factor of 0.75 means each octave will now have
3901+
* 75% impact (25% less) of the previous lower octave. Any value between
3902+
* 0.0 and 1.0 is valid, however note that values greater than 0.5 might
3903+
* result in greater than 1.0 values returned by <b>noise()</b>.<br /><br
3904+
* />By changing these parameters, the signal created by the <b>noise()</b>
3905+
* function can be adapted to fit very specific needs and characteristics.
3906+
*
3907+
* ( end auto-generated )
3908+
* @webref math:random
3909+
* @param lod number of octaves to be used by the noise
3910+
* @see PApplet#noise(float, float, float)
3911+
*/
37993912
public void noiseDetail(int lod) {
38003913
if (lod>0) perlin_octaves=lod;
38013914
}
38023915

3916+
/**
3917+
* @see #noiseDetail(int)
3918+
* @param falloff falloff factor for each octave
3919+
*/
38033920
public void noiseDetail(int lod, float falloff) {
38043921
if (lod>0) perlin_octaves=lod;
38053922
if (falloff>0) perlin_amp_falloff=falloff;
38063923
}
38073924

3808-
public void noiseSeed(long what) {
3925+
/**
3926+
* ( begin auto-generated from noiseSeed.xml )
3927+
*
3928+
* Sets the seed value for <b>noise()</b>. By default, <b>noise()</b>
3929+
* produces different results each time the program is run. Set the
3930+
* <b>value</b> parameter to a constant to return the same pseudo-random
3931+
* numbers each time the software is run.
3932+
*
3933+
* ( end auto-generated )
3934+
* @webref math:random
3935+
* @param seed seed value
3936+
* @see PApplet#noise(float, float, float)
3937+
* @see PApplet#noiseDetail(int, float)
3938+
* @see PApplet#random(float,float)
3939+
* @see PApplet#randomSeed(long)
3940+
*/
3941+
public void noiseSeed(long seed) {
38093942
if (perlinRandom == null) perlinRandom = new Random();
3810-
perlinRandom.setSeed(what);
3943+
perlinRandom.setSeed(seed);
38113944
// force table reset after changing the random number seed [0122]
38123945
perlin = null;
38133946
}

0 commit comments

Comments
 (0)