Graal Forums Algorithm RNG that progressively gets harder the higher the number
 FAQ Members List Calendar Search Today's Posts Mark Forums Read

#1
08-23-2013, 12:35 PM
 Cubical Banned Join Date: Feb 2007 Posts: 1,348
Algorithm RNG that progressively gets harder the higher the number

 I currently need a way to create random number generation that gets progressively harder the higher the number.. example: You have the chance of getting a number 0-10. You are most likely to get 1, less likely to get 2, even less likely to get 3 and so on. Does anyone have any suggestions on how to do where it would be progressively harder each number you go up or would my best bet be to do a bunch of logic checks to see if a random number is within a set range and iteration lessen the the gap that allows it to proceed to the next iteration until I've hit the whatever the max number?
#2
08-23-2013, 03:15 PM
 DrakilorP2P Registered User Join Date: Apr 2006 Posts: 755
 Sounds a bit like "normal distribution", or "the bell curve." You can generate numbers that act like that using the "Box–Muller transform." Look up these things. My attempt at implementing it in GScript: PHP Code: ``` function normal() {   temp.e = 2.7182818284590452353602874713526624977572470936999;   return cos(2*pi*random(0, 1)) * (-2 * log(temp.e, random(0, 1)))^0.5; }  ``` It generates numbers that are more likely to be close to 0. If you know in advance that you only need numbers 1 through 10 it's probably easier to do it another way.
 __________________ Testbed user: I figured since I can never find any scripters it was time to take desperate measures...and...TEACH MYSELF 0.0
#3
08-23-2013, 04:03 PM
 cbk1994 the fake one Join Date: Mar 2003 Location: San Francisco Posts: 10,718
Quote:
 Originally Posted by DrakilorP2P Sounds a bit like "normal distribution", or "the bell curve." You can generate numbers that act like that using the "Box–Muller transform." Look up these things.
This is what we did on Kingdoms for the mystery boxes (for determining which item you got). If you need more code samples, I can dig up our implementation as well.
 __________________
#4
08-23-2013, 04:14 PM
 Cubical Banned Join Date: Feb 2007 Posts: 1,348
 I'll take a look at it when I get home and see if I can get a general understanding on what he provided and if not I'll take you up on that probably.
#5
09-01-2013, 03:03 AM
 Loriel Somewhat rusty Join Date: Mar 2001 Posts: 5,059
 wow i'm bad with statistics. apparently you can transform your rand()'s uniform distribution into a desired distribution via the latter's quantile function which is the inverse of the cumulative distribution function which is the integral of the probability density function and you could pick something like 2*(1-x) as your density function (for 0<=x<1, i guess the idea is that it's normalized). then wolfram alpha gives 1-sqrt(1-x) (the + solution sounds useless i dunno). so you'd transform the uniform 0<=x<1 rand() into a number between 0 and 10 by doing (1-sqrt(1-rand()))*11 and rounding down. i guess if 1-x doesn't appeal to you as a distribution you can do the whole thing with stuff like 1-sqrt(x) and 1-x*x and that sort of thing and see if something cool happens but wow i hate statistics.
#6
09-01-2013, 11:00 AM
 Crono :pluffy: Join Date: Feb 2002 Location: Sweden Posts: 20,000
Quote:
 Originally Posted by Loriel wow i'm bad with statistics. apparently you can transform your rand()'s uniform distribution into a desired distribution via the latter's quantile function which is the inverse of the cumulative distribution function which is the integral of the probability density function and you could pick something like 2*(1-x) as your density function (for 0<=x<1, i guess the idea is that it's normalized).
wat
 __________________
#7
09-01-2013, 01:02 PM
 xXziroXx Master of Puppets Join Date: May 2004 Location: Sweden Posts: 5,288
 Good seeing you as usual Loriel.
 __________________ Contact Informationemail: [email protected] "A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto
#8
09-02-2013, 02:36 AM
 dylan AGT Join Date: Jul 2012 Location: United States Posts: 60
 I was trying to do something to where the chance exponentially gets higher or lower, but it seems the Math.pow(int, int) function no longer is in working fashion.
 __________________ <@Logan> I spent a minute looking at my own code by accident. <@Logan> I was thinking "What the hell is this guy doing?"
#9
09-02-2013, 03:18 AM
 going off loriel's post... I came up with: PHP Code: ``` function onCreated() {  temp.tmax = 10;  temp.rand = function (m) { return int(((1-random(0,1)^0.5)) * m) };    for (temp.i = 0; temp.i < 1000; temp.i++) {    temp.("count_" @ temp.rand(temp.tmax))++;  }  for (temp.i = 0; temp.i < temp.tmax; temp.i++) {    echo(temp.i SPC temp.("count_" @ temp.i));  }}/*Sample:0 1791 1702 1453 1264 1155 956 757 598 279 9*/  ``` one other way I tried: PHP Code: ``` function onCreated() {  temp.tmax = 10;  temp.rand = function (m) {    temp.i = 0;    while (random(0,1) > 0.5 && temp.i < m) {      temp.i++;    }    return temp.i;  };    for (temp.i = 0; temp.i < 1000; temp.i++) {    temp.("count_" @ temp.rand(temp.tmax))++;  }  for (temp.i = 0; temp.i < temp.tmax; temp.i++) {    echo(temp.i SPC temp.("count_" @ temp.i));  }}/*Sample:0 5201 2432 1323 564 275 126 27 38 39 1*/  ``` I'm sure there's a math way to do it but **** it.
__________________
Quote:
#10
09-02-2013, 03:23 AM
 cbk1994 the fake one Join Date: Mar 2003 Location: San Francisco Posts: 10,718
Quote:
 Originally Posted by dylan I was trying to do something to where the chance exponentially gets higher or lower, but it seems the Math.pow(int, int) function no longer is in working fashion.
You can simply use a caret in GScript—it's not used for xor like it is in most languages.
 __________________
#11
09-02-2013, 03:43 PM
 scriptless Banned Join Date: Dec 2008 Location: N-Pulse Posts: 1,412
 This sounds interesting. Would defiantly make the daily freebies system more efficient. Is this also used in something like rarity of item spawns/drops?
#12
09-02-2013, 04:30 PM
 Loriel Somewhat rusty Join Date: Mar 2001 Posts: 5,059
Quote:
 Originally Posted by fowlplay4 going off loriel's post... I came up with: PHP Code: ``` (1-random(0,1)^0.5)  ```
It took me way too long to figure out why 1-sqrt(rand()) is the same thing as 1-sqrt(1-rand())