Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Random weighted number between two numbers?  (Read 3512 times) 0 Members and 1 Guest are viewing this topic.
Emmsii
 « Posted 2017-04-14 08:12:20 »

Here's my problem.

I have two numbers: 1.1501344 & 1.1816746. I'd like to generate a random number between these two numbers, but weighted to the 'center' of the number: 1.1659045. So there is a higher chance of the random number being closer to the 1.16 value, rather than the 1.15 & 1.18 values.

I've tried using random.nextGaussian() but that creates values < -1 and > +1.

Any ideas?
Roquen

JGO Kernel

Medals: 517

 « Reply #1 - Posted 2017-04-14 09:33:34 »

The easiest the triangle distribution (https://en.wikipedia.org/wiki/Triangular_distribution), but it depends on how you what distribution curve to look.
Roquen

JGO Kernel

Medals: 517

 « Reply #2 - Posted 2017-04-14 09:48:51 »

Of course wikipedia is about as clear as ever:

 1  2  3  4  5  6  7  8  9  10  11  12  13 `// symmetric triangle distribution: result on (-1,1)public static float symmetricTriangular(){   return nextFloat()-nextFloat());  // from random number generator of choice}// symmetric triangle distribution: result on [lo, hi)public static float symmetricTriangular(float lo, float hi){    float halfDiff = 0.5f*(hi-lo);    float triDist  = nextFloat()+nextFloat();   // triangle on [0,2)    return halfDiff*triDist + lo;}`
KaiHH

JGO Kernel

Medals: 511

 « Reply #3 - Posted 2017-04-14 10:05:34 »

I've tried using random.nextGaussian() but that creates values < -1 and > +1.
You could still use a Gaussian distribution, or any distribution generating values within [A, B], and do a simple linear interval mapping to [a, b] (i.e. "stretching the interval") via:
 1  2  3  4  5 `double val = Random.nextGaussian();double a = 1.1501344; // the min value of our target intervaldouble b = 1.1816746; // the max value of our target intervaldouble value = (val + 1.0) * (b - a) / 2.0 + a; // <- interval mapping from [-1, +1] -> [a, b]// value is the gaussian distribution within [a, b]`
Emmsii
 « Reply #4 - Posted 2017-04-14 10:11:47 »

Thanks for those, I'll give them both a go.

Quick question, where did you get the 'val' value from?  Would that be the 'center' value of 1.165 I mentioned above?
 1 `double value = (val + 1.0) * (b - a) / 2.0 + a; // <- interval mapping from [-1, +1] -> [a, b]`

KaiHH

JGO Kernel

Medals: 511

 « Reply #5 - Posted 2017-04-14 10:18:07 »

Quote
Quick question, where did you get the 'val' value from?
Sorry, modified the post. It is of course the value you want to map. I just copied the interval mapping code from some SF answer.
Roquen

JGO Kernel

Medals: 517

 « Reply #6 - Posted 2017-04-14 11:03:14 »

Note that the Gaussian distribution isn't bounded...not sure if that's what you want.
Pages: [1]
 ignore  |  Print

 ivj94 (583 views) 2018-03-24 14:47:39 ivj94 (47 views) 2018-03-24 14:46:31 ivj94 (374 views) 2018-03-24 14:43:53 Solater (61 views) 2018-03-17 05:04:08 nelsongames (108 views) 2018-03-05 17:56:34 Gornova (150 views) 2018-03-02 22:15:33 buddyBro (693 views) 2018-02-28 16:59:18 buddyBro (91 views) 2018-02-28 16:45:17 xxMrPHDxx (493 views) 2017-12-31 17:17:51 xxMrPHDxx (732 views) 2017-12-31 17:15:51
 SHC 10x NuclearPixels 10x Zemlaynin 10x KaiHH 10x ByerN 7x Spasi 6x Guerra2442 6x Damocles 5x VaTTeRGeR 5x ags1 4x orangepascal 4x philfrei 4x princec 3x ndnwarrior15 3x mesterh 3x Phased 2x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org