Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) 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 3785 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: 520

 « 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: 520

 « 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

 EgonOlsen (79 views) 2018-06-10 19:43:48 EgonOlsen (59 views) 2018-06-10 19:43:44 EgonOlsen (78 views) 2018-06-10 19:43:20 DesertCoockie (261 views) 2018-05-13 18:23:11 nelsongames (159 views) 2018-04-24 18:15:36 nelsongames (158 views) 2018-04-24 18:14:32 ivj94 (901 views) 2018-03-24 14:47:39 ivj94 (162 views) 2018-03-24 14:46:31 ivj94 (813 views) 2018-03-24 14:43:53 Solater (177 views) 2018-03-17 05:04:08
 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