处理,nextGaussian不在-1和1之间?

时间:2013-09-07 21:20:54

标签: processing

我正在开发一个小的Processing项目来显示高斯随机数,我有一个问题,即产生的数字不在-1和1之间,这会抛弃我的整个事情。我需要用什么数字来缩放数字以将其缩放到-1到1范围?

程序

import java.util.*;
Random generator;

void setup()
{
  size(600,600);
  generator = new Random();
  background(255);
}

void draw()
{
  generatenewdot();
}

void generatenewdot()
{
  float xnum = (float)generator.nextGaussian();
  float sd = 60;
  float xmean = width/2;

  float x = sd * xnum + xmean;

  float ynum = (float)generator.nextGaussian();
  float ymean = height/2;

  float y = sd * ynum + ymean;

  //Bigger when closer, smaller when farther
  //get distance from center

  float distance = sqrt(pow(sd*xnum,2) + pow(sd*ynum,2));
  float size = (1 - (distance/sd)) * 10;

  println("X: " + xnum + " | Y: " + ynum);

  noStroke();
  fill(0, 50);
  ellipse(x, y, size, size);
}

输出

X: -1.2560346 | Y: 0.43913
X: 1.6684186 | Y: -1.0307527
X: -0.20982298 | Y: -0.22280085
X: -0.5874519 | Y: -2.3560321
X: -1.6996952 | Y: -0.6252706
X: -0.3767569 | Y: 0.8847021
X: 2.16339 | Y: 0.84067136
X: 0.9579867 | Y: -1.5785545
X: 0.39750758 | Y: -1.4931071
X: -0.43088776 | Y: 0.75665843
X: 1.0801688 | Y: -2.6318247
X: -1.1454289 | Y: -0.95125073
X: 1.4965589 | Y: 1.0736477
X: -0.39217913 | Y: -0.34573647
X: 0.0060171164 | Y: 1.0194058
X: -2.482392 | Y: -0.37381676
X: -0.120092504 | Y: -1.3072939
X: -0.79035485 | Y: -0.33741793
X: -1.0654978 | Y: -1.3986521
X: 0.30195275 | Y: -0.09365952
X: -1.3775821 | Y: -0.91224575
X: 0.15109988 | Y: -1.5025057
X: -0.9888884 | Y: 0.5061488
X: 1.7362484 | Y: -0.3089954
X: -0.48933098 | Y: -1.0289067
X: 0.54819685 | Y: 1.915066
X: -0.89757615 | Y: -0.5725651
X: -1.1072423 | Y: 0.1549204
X: -0.05344215 | Y: 0.044364702
X: -0.047609538 | Y: 0.7296939
X: 0.62724686 | Y: 0.30536157
X: 1.6744004 | Y: 0.13948894
X: 0.8259148 | Y: 1.3571945
X: 0.93188906 | Y: 0.89352655
X: -0.62066746 | Y: -0.2791857
X: 0.45208132 | Y: 0.7484442
X: 1.4533566 | Y: 0.15946567
X: -0.26117975 | Y: -0.9406836
X: 0.14579183 | Y: -0.6735571
X: -0.11575534 | Y: 0.24251445
X: -0.71969754 | Y: 1.2312696
X: -0.33103114 | Y: -0.3679331
X: -0.87640345 | Y: 0.18695408
X: 1.4733391 | Y: -1.717308
X: -0.47379303 | Y: 0.94097525
X: -1.2317376 | Y: -0.44845966
X: -1.9225131 | Y: 0.33214918
X: 0.6805828 | Y: -0.82625526
X: 2.0353675 | Y: -0.56851065
X: 0.43589613 | Y: -0.059929434
X: -0.53751755 | Y: 0.85414284
X: 2.2904902 | Y: -0.369723
X: -0.12156449 | Y: -1.0917004
X: -0.889154 | Y: 0.64644057
X: -0.22776474 | Y: 1.4512538
X: -0.7672019 | Y: -0.42612842
X: -0.410998 | Y: -1.1754745
X: 0.9694859 | Y: -0.32890838
X: 0.38745773 | Y: -0.7073066
X: -0.41483563 | Y: 0.3499609
X: 0.0570873 | Y: 1.0256606
X: 0.5236664 | Y: -1.6412264
X: -1.1053294 | Y: -0.06714651
X: 0.77513236 | Y: -0.36635277
X: 0.40660658 | Y: 0.72002655
X: -0.2413241 | Y: 0.10751419
X: -0.5079167 | Y: 0.2544589
X: 0.0738618 | Y: -0.46861497
X: 0.36933857 | Y: 3.282707
X: -0.38261575 | Y: -1.2680397
X: 0.26502755 | Y: -1.4085288
X: -1.0758035 | Y: 0.48999637
X: -1.0022781 | Y: 0.18629716
X: 1.089121 | Y: 0.22576475
X: -0.1774196 | Y: 1.1405197
X: -0.75939226 | Y: -1.246858
X: 2.0534744 | Y: -0.027038628
X: 1.2573285 | Y: -0.2153611
X: 0.91109043 | Y: -0.9592889
X: 1.1948314 | Y: 0.8129871
X: -0.2642877 | Y: -2.1204646
X: 0.76492834 | Y: 1.0202236
X: -0.19996478 | Y: -0.25985986
X: 0.305574 | Y: -0.15428662
X: -0.5793801 | Y: 0.05751243
X: -1.1586995 | Y: -1.354029
X: -2.0605218 | Y: 0.15988825
X: 0.13619052 | Y: 0.688434
X: -0.28077507 | Y: -1.5288342
X: -0.382878 | Y: -0.37027964
X: 0.7066691 | Y: -0.71114177
X: 0.3983471 | Y: 0.52614754
X: -0.7913421 | Y: 1.4425266
X: 0.12273746 | Y: 1.081629
X: -0.13688068 | Y: -1.1057968
X: 0.60105777 | Y: -0.93368775
X: -1.3000423 | Y: -0.061667506
X: 0.12970658 | Y: -0.82605433
X: 0.29501432 | Y: -0.52549535
X: 0.39391693 | Y: 0.8624781
X: 2.4755092 | Y: -1.0342329
X: -0.22210532 | Y: -0.6144739
X: -0.3413467 | Y: -0.19307336
X: -0.5308936 | Y: 0.30849203
X: 1.1740618 | Y: 1.2144334
X: 0.1827944 | Y: -0.17112117
X: 1.1925478 | Y: -0.3520857
X: -1.2051786 | Y: 0.15042834
X: 3.0183444 | Y: -0.90113115
X: -1.0622096 | Y: -0.30049986
X: 1.8420129 | Y: 1.2509897
X: -2.297314 | Y: -2.0725875
X: 1.0673923 | Y: -2.5084429
X: 0.3134048 | Y: -0.052358847
X: 0.16638549 | Y: 0.5592031
X: -0.067902535 | Y: -1.420417
X: -0.016045086 | Y: -1.0686399
X: -0.6912575 | Y: -0.9322265
X: 1.0217631 | Y: 0.5885641
X: -0.77907616 | Y: 0.5000267
X: 0.01890201 | Y: -0.11718043
X: -0.958193 | Y: -1.3351127
X: -0.3736327 | Y: 1.9608271
X: -0.7962202 | Y: 0.7476578
X: 0.25758967 | Y: -0.77497977
X: -0.48639807 | Y: -0.7017801
X: -0.82916087 | Y: 0.8303733
X: -1.707152 | Y: -1.3296013
X: -1.7046789 | Y: -0.24234797
X: -1.0117844 | Y: 0.45633218
X: -0.08127227 | Y: 0.48397893
X: 0.82085145 | Y: 2.2481968
X: 1.3258406 | Y: 2.198357
X: 0.2562196 | Y: 0.3340968
X: 0.60788757 | Y: 0.24063592
X: -0.031290732 | Y: -0.14278243
X: -1.2028422 | Y: 0.6705914
X: -0.7122825 | Y: 0.33648172
X: -1.3599969 | Y: -0.6731673
X: -0.5936929 | Y: -0.34959924
X: 1.964009 | Y: 0.8512692
X: -0.68523324 | Y: -0.4050971
X: 0.60271347 | Y: -0.09168337
X: 0.02265682 | Y: -0.8994877
X: 0.5623386 | Y: -1.1070106
X: -1.4453007 | Y: -0.31628308
X: -0.062762 | Y: 1.5903912
X: -0.3273365 | Y: 0.11271362
X: -0.011844525 | Y: -0.97879356
X: -0.21651044 | Y: -1.488296
X: 1.7444072 | Y: -0.6698305
X: 0.07018017 | Y: 0.052752882
X: 0.33038074 | Y: -0.51466936
X: -0.3488621 | Y: 0.6184827
X: 0.049165536 | Y: 0.7268606
X: 0.38889283 | Y: 0.22207315
X: 1.1190308 | Y: 0.18530433
X: -0.5596919 | Y: 0.038130693
X: 2.2971296 | Y: 0.08598893
X: 0.48901424 | Y: -0.6340521
X: -0.34295475 | Y: 2.4535484
X: 0.7059389 | Y: -0.060639072
X: -0.9430291 | Y: -0.030210787
X: 0.14106135 | Y: 0.3733378
X: 0.23838589 | Y: -0.11811932
X: -0.14800806 | Y: 0.02691031
X: -0.4372808 | Y: -0.912786
X: -1.2872834 | Y: -0.16293962
X: 1.9569577 | Y: -0.2684084
X: 1.4659501 | Y: 0.79093254
X: 1.1387724 | Y: 1.4153944
X: -0.9614859 | Y: 1.2193747
X: -0.24685526 | Y: 1.1087464
X: 0.25005496 | Y: -0.18707326
X: -0.08282836 | Y: 0.16380334
X: -0.890932 | Y: -0.01911562
X: -1.3180755 | Y: -1.7750782
X: -1.9738325 | Y: 0.4650801
X: 0.11823471 | Y: -0.7086047
X: 0.7637192 | Y: 3.0014162
X: 1.0746229 | Y: 0.5347033
X: -0.57545507 | Y: 1.2818112
X: 1.2836952 | Y: 1.0796871
X: -0.26668364 | Y: 0.19159853
X: -2.1289194 | Y: -1.8146951
X: 1.4087044 | Y: 0.266897
X: -0.12748817 | Y: 1.4472772
X: 1.0245978 | Y: 0.17778511
X: -1.416702 | Y: -1.1412634
X: -1.5589409 | Y: 0.095859885
X: 1.1145445 | Y: -0.32158515
X: -2.2714367 | Y: -1.032223
X: 1.2649084 | Y: -1.9037691
X: -0.33965677 | Y: 0.81614226
X: -0.12146729 | Y: 0.4777388
X: -0.4253145 | Y: 1.5192709
X: -0.20069526 | Y: 0.0266705
X: 0.7794582 | Y: 0.20600611
X: 0.068017714 | Y: -0.7936217
X: -0.8899986 | Y: -0.5964193
X: -0.18383493 | Y: -0.92586005
X: -0.7818783 | Y: -0.4391756
X: 2.9518538 | Y: 0.51974326
X: -0.30460843 | Y: 1.4595356
X: -0.76000154 | Y: -0.6935336
X: 1.2247328 | Y: -1.9350185
X: -0.17959218 | Y: 0.52692646
X: -1.1845113 | Y: -0.7372843
X: 0.7145226 | Y: 0.97987723
X: 0.294313 | Y: 0.42491102
X: 0.69715863 | Y: 1.0249106
X: 0.61984223 | Y: 1.9051309
X: 0.10692853 | Y: -0.7397708
X: -1.6503758 | Y: -0.56400114
X: -0.619303 | Y: -0.50828993
X: -1.0103049 | Y: 1.5179138
X: -0.6610786 | Y: -0.23466662
X: 1.47647 | Y: -0.7518148
X: 1.8461105 | Y: -0.89638317
X: -1.0333326 | Y: 0.5272911
X: 0.32953307 | Y: -2.068906
X: -0.3369011 | Y: 1.1342978
X: 0.26832464 | Y: 0.35111254
X: -1.1943347 | Y: 1.5756301
X: -0.2821439 | Y: -0.21851084
X: 0.045426905 | Y: 0.43564394
X: -0.08327247 | Y: 0.17497726
X: 0.26342446 | Y: 0.8225831
X: 0.51339006 | Y: -0.70085526
X: 0.6015727 | Y: 1.4097772
X: -0.87026924 | Y: -1.2695398
X: 0.9147027 | Y: -0.54681355
X: -0.25914836 | Y: 0.14173253
X: 0.79905576 | Y: -0.78842294
X: -0.122037366 | Y: 1.9061232
X: 0.031827897 | Y: 9.054022E-5
X: 0.8216564 | Y: 1.1695337
X: -0.3981461 | Y: 1.6214145
X: -0.6744849 | Y: 0.16823441
X: 0.1994377 | Y: 1.041054
X: -0.9688934 | Y: 0.5749102
X: 0.52517 | Y: 0.3087776
X: -0.31726742 | Y: 1.0651228
X: 0.5944692 | Y: -0.39537027
X: -0.14272667 | Y: 0.15524986
X: 1.0194787 | Y: -0.17233147
X: -0.3691948 | Y: 0.62380767
X: 1.2492002 | Y: -0.024808018
X: 1.0656878 | Y: -0.6394374
X: 1.8846616 | Y: 1.1887405
X: 2.2143235 | Y: -0.9957973
X: 1.1480668 | Y: -2.0087852
X: -0.7856953 | Y: 0.17126346
X: 1.4994434 | Y: 0.42100754
X: 0.61520094 | Y: 0.4594768
X: -1.8998433 | Y: 0.81686187
X: -0.59870094 | Y: 0.16190825
X: 0.7568211 | Y: 0.1445908
X: 0.068865344 | Y: -0.11507618
X: -0.13646689 | Y: -0.71280926
X: -0.42065284 | Y: 1.6816016
X: -0.8548044 | Y: -0.23868565
X: 0.44674578 | Y: -0.08002255
X: 0.15143052 | Y: 0.9913837
X: -0.24043444 | Y: -0.4339675
X: -0.07372746 | Y: -1.4390934
X: -2.0154748 | Y: -0.6124442
X: 2.336962 | Y: 0.09206108
X: 0.12995008 | Y: 0.6019416
X: -0.14780447 | Y: -0.9132321
X: 0.8062503 | Y: 0.1129237
X: 0.12131127 | Y: -1.5861236
X: 0.4869577 | Y: -0.67768985
X: -1.1218196 | Y: 1.184578
X: 0.7874171 | Y: -0.045187805
X: -0.67828524 | Y: -0.5735016
X: -0.994444 | Y: 0.28350237
X: 2.633789 | Y: 0.54614604
X: -0.08738608 | Y: 1.5393788
X: -0.1632809 | Y: 1.0571663
X: -0.9038875 | Y: 0.30314434
X: 1.3459784 | Y: 0.38406494
X: -0.638972 | Y: -0.21715638
X: 1.401298 | Y: 0.2867331
X: -0.92279404 | Y: 0.9176272
X: -0.041815635 | Y: -1.5927134
X: 0.14853486 | Y: -0.5005275
X: -0.12517829 | Y: 0.9995699
X: 0.30420595 | Y: -0.7695801
X: -1.1811762 | Y: -1.1573164
X: 0.3598652 | Y: 0.18536983
X: 0.92923003 | Y: -0.045854274
X: 1.0242708 | Y: -0.6519265
X: -0.15148447 | Y: -1.6188399
X: -0.5767954 | Y: -0.7759711
X: 1.691366 | Y: 2.0550196
X: -2.4651797 | Y: -0.532277
X: -0.38202325 | Y: 2.7200665
X: -0.026308075 | Y: -0.41392756
X: -0.54529566 | Y: 1.0306138
X: -0.2474008 | Y: -0.8410272
X: -0.8878296 | Y: 0.6692659
X: -1.0749551 | Y: -0.05105501
X: -1.4509034 | Y: -0.40763706
X: 0.63799614 | Y: 0.5781374
X: -0.15311946 | Y: 0.9867796
X: 1.0860987 | Y: 2.2270775
X: -1.8498964 | Y: -0.15837035
X: 0.5712155 | Y: 0.6274683
X: 0.10293772 | Y: -0.2556146
X: 0.70576346 | Y: -0.60659826
X: 0.40139621 | Y: 0.17961529
X: -1.8881758 | Y: 0.017092947
X: -2.8272333 | Y: -0.9225435
X: -0.10255992 | Y: -0.5627598
X: -1.5514668 | Y: 1.090334
X: 0.8607706 | Y: 0.44446054

1 个答案:

答案 0 :(得分:0)

高斯概率分布扩展到无穷大,但您可以对其进行缩放,以使样本在(-1,1)中具有95%,99%或更高的概率。但有些样本总是在(-1,1)之外。

例如generator.nextGaussian()/1.69将在(-1,1)中,概率为95%,外部概率为5%。