Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (813)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Random weighted number between two numbers?  (Read 2141 times)
0 Members and 1 Guest are viewing this topic.
Offline Emmsii

Senior Devvie


Medals: 9
Projects: 2



« 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?
Offline Roquen

JGO Kernel


Medals: 514



« 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.
Offline Roquen

JGO Kernel


Medals: 514



« 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;
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 442



« 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 interval
double b = 1.1816746; // the max value of our target interval
double value = (val + 1.0) * (b - a) / 2.0 + a; // <- interval mapping from [-1, +1] -> [a, b]
// value is the gaussian distribution within [a, b]
Offline Emmsii

Senior Devvie


Medals: 9
Projects: 2



« 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]

Offline KaiHH

JGO Kernel


Medals: 442



« 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.
Offline Roquen

JGO Kernel


Medals: 514



« 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  
 
 

 
cybrmynd (125 views)
2017-08-02 12:28:51

cybrmynd (149 views)
2017-08-02 12:19:43

cybrmynd (143 views)
2017-08-02 12:18:09

Sralse (159 views)
2017-07-25 17:13:48

Archive (631 views)
2017-04-27 17:45:51

buddyBro (751 views)
2017-04-05 03:38:00

CopyableCougar4 (1285 views)
2017-03-24 15:39:42

theagentd (1248 views)
2017-03-24 15:32:08

Rule (1225 views)
2017-03-19 12:43:22

Rule (1300 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!